Build/Test Issues

TestCaseSource code always shows empty coverage

Started by jnm236 on 4,713 views

NCrunch shows code coverage dots for the lines in the AllDbContexts method but they are always empty.
NUnit executes this method during test discovery and then NCrunch only tracks coverage in the execution stage where it does not rerun AllDbContexts.

Ideally NCrunch would track the execution of AllDbContexts, even if that means rediscovering the list of generated tests every time you run any tests.


[TestFixture]
public class Tests
{
    public static IEnumerable<Type> AllDbContexts() =>
        from type in typeof(x).Assembly.GetTypes()
        where type.IsSubclassOf(typeof(DbContext)) && !type.IsAbstract
        select type;

    [TestCaseSource(nameof(AllDbContexts))]
    public void Test(Type dbContextType)
    {
    }
}
Unfortunately, there really isn't any way that NCrunch could safely track the coverage of this initialiser.

Beyond being executed in the test discovery step, this code is run as part of a static constructor. Static constructors are usually executed by the CLR the first time a class is referenced, which in this case is entirely outside of the control of NCrunch as it is probably first touched by NUnit when it loads the assembly.

Capturing code coverage during test discovery is also problematic because of the timing of test generation. At the time the coverage would need to be captured, the tests would not yet exist as NUnit wouldn't have reported them yet. Running the test discovery twice every time would greatly diminish performance, even in situations where code like this wasn't being used.

For now, the best solution here is to avoid complex code in your test generation.

Post a reply

Log in to reply.