This is a simplified version of some code I have:
public class FixtureData
{
public object SomeValue { get; set; }
}
public class TestForNull : IClassFixture<FixtureData>
{
private readonly FixtureData _data;
public TestForNull(FixtureData data)
{
_data = data;
}
[Fact]
public void TestForNull()
{
_data.SomeValue = null;
Assert.Null(_data.SomeValue);
}
}
public class TestForObject : IClassFixture<FixtureData>
{
private readonly FixtureData _data;
public TestForObject(FixtureData data)
{
_data = data;
}
[Fact]
public void TestForObject()
{
Assert.NotNull(_data.SomeValue);
}
}
Both classes are not marked with any collection attribute. They both belong to the same assembly.
I am seeing these test fail (but only sporadically), which can only be explained by XUnit sharing the FixtureData instance across test classes, and TestForNull running first (since it has side effects).
However, the XUnit documentation makes it clear that class fixtures are to "shared object instance across tests in a single class".
Is this a bug? Should I change the way in which I'm using the fixtures?
I'm using xUnit for .NET Core 2.3.1.