Daily Usage Issues

Static init code coverage is suppressed when TestCaseSource is used

Started by GreenMoose on 1,192 views

[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
[img=https://i.imgur.com/aDzHAJx.png]Scenario1[/img]

Scenario 2
[img=https://i.imgur.com/8rFaNdK.png]Scenario 2[/img]


[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);
    }
}
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.

Post a reply

Log in to reply.