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

Notification

Icon
Error

Static init code coverage is suppressed when TestCaseSource is used
GreenMoose
#1 Posted : Wednesday, October 2, 2024 12:18:11 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 507

Thanks: 145 times
Was thanked: 66 time(s) in 64 post(s)
[v5.6.0.1]

When using TestCaseSource, I cannot get coverage for static initialization code (running test in new process). When not using TestCaseSource, it works as expected.
Is this expected behavior?


Scenario 1
Scenario1

Scenario 2
Scenario 2

Code:

[TestFixture]
internal class FixtureWithStaticInit 
{
    private static int _counter;
    private static int[] _testCaseSource = new int[] { 1, 2 };

    //[TestCaseSource(nameof(_testCaseSource))]
    public void Test2(int tc)
    {
        Console.WriteLine($"Counter: {_counter}.");
        Console.WriteLine($"TC: {tc}.");
    }

    [Test]
    public void Test1()
    {
        Console.WriteLine($"Counter: {_counter}.");
    }

    static FixtureWithStaticInit()
    {
        Interlocked.Increment(ref _counter);
    }
}
Remco
#2 Posted : Wednesday, October 2, 2024 11:13:42 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,161

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Hi, yes this is behaviour as designed.

We don't track code coverage data inside the analysis/discovery step. Because the static initializers are technically housed inside the static constructor for the class, they get executed during discovery. Generally, we re-use the test process that was used to discover the tests, so the CLR won't call the static constructor again during execution.

Static constructors in general will always give inconsistent behaviour around test coverage as their lifecycles do not align with test execution.
1 user thanked Remco for this useful post.
GreenMoose on 10/3/2024(UTC)
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.024 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download