When an xUnit2 Testclass uses a ClassFixture, the expectation/requirement is that only one Fixture instance is created for all tests contained within it.
nCrunch (v2.10.0.4) consistently calls the constructor twice, even if only one Test is actually present (but it should only be called once regardless of how many tests there are).
I have included a sample that demonstrates the problem, it passes with the xUnit VSTestadapter (build 99.8) but fails in nCrunch.
Furthermore, it appears dispose is only called once for the Fixture (see debug output). I'm not sure how to write that as a unit test though (and it should fix itself once ctor is only called once).
I assume this is a bug? Thanks for your input on this.
The output from nCrunch is:
On the Class Node:
Fixture.ctor
Fixture.ctor
Fixture.Dispose
Child test failed
And on the Test:
Xunit.Sdk.EqualException: Assert.Equal() Failure
Expected: 1
Actual: 2
Full sample
using System;
using System.Diagnostics;
using System.Threading;
using Xunit;
namespace NCrunchTest
{
public class Fixture : IDisposable
{
public static int CtorCallCount;
public static int DisposeCallCount;
public Fixture()
{
Debug.WriteLine("Fixture.ctor");
Interlocked.Increment(ref CtorCallCount);
}
public void Dispose()
{
Debug.WriteLine("Fixture.Dispose");
Interlocked.Increment(ref DisposeCallCount);
}
}
public class FixtureTests : IClassFixture<Fixture>
{
Fixture fixture;
public FixtureTests(Fixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void FixtureConstructurCalledOnlyOnce()
{
Assert.Equal(1, Fixture.CtorCallCount);
}
// can even comment this test out
[Fact]
public void FixtureConstructurCalledOnlyOnce_2()
{
Assert.Equal(1, Fixture.CtorCallCount);
}
}
}