Hi there,
I am currently facing a problem with an ICollectionFixture in xunit and I am not sure if this is an issue in the NCrunch engine or if I am simply missing some magic configuration I did not discover yet.
The fixture is shared with all test classes through a collection, so I would expect it to behave like a singleton. When executing with Visual Studio, it behaves as I would expect (only one instance of the fixture is instantiated throughout test execution).
But running with NCrunch, the fixture gets instantiated multiple times*). Parallel test execution is explicitly disabled. I could strip this behavior down to a project with only the following code:
Code:
[Collection("TestCollection")]
public class UnitTest1
{
private readonly Fixture fixture;
public UnitTest1(Fixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void Test1()
{
}
}
[Collection("TestCollection")]
public class UnitTest2
{
private readonly Fixture fixture;
public UnitTest2(Fixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void Test2()
{
}
}
public class Fixture
{
private static int counter = 0;
public Fixture()
{
int newCount = Interlocked.Increment(ref counter);
if (newCount > 1)
throw new Exception($"{newCount} instances? WTF???");
}
}
[CollectionDefinition("TestCollection")]
public class TestCollectionDefinition : ICollectionFixture<Fixture>
{
}
Thanks for any support,
Julian
*) More details on NCrunch behavior based on the sample code:
- When starting all tests in Debug, it works.
- When all tests have the same state before execution, the execution works too.
- When one test is red, one is green, the Exception in the fixture's ctor is thrown on that test that previously was green. Further executing all tests lets both facts toggle between success and failure, meaning: The test, which was in failing state gets the first fixture instance, the succeeding test gets the second fixture instance. Feels like it depends if there are multiple execution queues/priorities of the tests?
P.S.: NCrunch version is 4.6.0.3.