Improving C# test coverage

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:

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

[Test]
public void Test_All()
{
    Type entry = typeof(TestConfigRaw); // set a class contained in
// the assembly you want to test
    Assembly asm = entry.Assembly;
 
    foreach (Type type in asm.GetTypes())
    {
        try
        {
            foreach (ConstructorInfo ctor in type.GetConstructors())
            {
                try
                {
                    List<object> args = new List<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 = new List<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:

And many feedbacks :

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

Laurent Vion

Source: Criteo