Improving C# test coverage

By: CriteoLabs / 01 Apr 2014

As all big companies, Criteo developers have to provide  unit tests to ensure a good code quality.

One month before my annual performance interview, and one week before my Christmas holidays, my manager pointed out to me that most of my C# repositories had a very low coverage:

image2014-3-31-16-22-57

After half a day of reflection about reflection, I added the following test to all my projects:

image2014-3-14-10-4-6

[Test]publicvoidTest_All(){
    Type entry =typeof(TestConfigRaw);// set a class contained in// the assembly you want to test
    Assemblyasm= entry.Assembly;
 
    foreach(Type type inasm.GetTypes())
    {
        try
        {
            foreach(ConstructorInfo ctor in type.GetConstructors())
            {
                try
                {
                    List<object> args =newList<object>();
 
                    foreach(ParameterInfo p in ctor.GetParameters())
                    {
                        Type pt = p.ParameterType;
                        if(pt.IsValueType)
                            args.Add(Activator.CreateInstance(pt));
                        else
                            args.Add(null);
                    }
 
                    object instance =Activator.CreateInstance(type, args.ToArray());
 
                    foreach(MethodInfo method in type.GetMethods())
                    {
                        try
                        {
                            List<object> parameters =newList<object>();
 
                            foreach(ParameterInfo p in method.GetParameters())
                            {
                                Type pt = p.ParameterType;
 
                                if(pt.IsValueType)                                 
                                    parameters.Add(Activator.CreateInstance(pt));
                                else                                  
                                    parameters.Add(null);
                            } 
                 
                            method.Invoke(instance, parameters.ToArray());
                        }
                        catch{}
                    }
                }
                catch{}
            }
        }
        catch{}
    }}

I got a 22% coverage increase:

image2014-3-31-16-23-41

And many feedbacks :

love

image2014-3-14-10-8-21

I’m now dragging all improvement suggestion to fill my backlog for the next Test sprint. If you have some ideas, let me know 🙂

Author: Laurent Vion

  • CriteoLabs

    Our lovely Community Manager / Event Manager is updating you about what's happening at Criteo Labs.


leo risus. commodo felis mattis odio libero. ipsum massa fringilla non