Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

TestCaseSource code always shows empty coverage
jnm236
#1 Posted : Tuesday, February 7, 2017 3:33:58 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/25/2015(UTC)
Posts: 57
Location: United States of America

Thanks: 1 times
Was thanked: 4 time(s) in 4 post(s)
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.

Code:

[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)
    {
    }
}
Remco
#2 Posted : Tuesday, February 7, 2017 9:53:19 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,964

Thanks: 929 times
Was thanked: 1255 time(s) in 1168 post(s)
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.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.029 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download