Is it possible to run tests in isolation with dotnetcore, dotnet5, or dotnet6?
Suppose I have a class named TheStaticFoo - a no-frills - a minimum working example:
public class TheStaticFoo
{
public TheStaticFoo() => Current = this;
private static TheStaticFoo _current;
public static TheStaticFoo Current
{
get => _current;
private set
{
if (_current != null)
throw new InvalidOperationException("Oh shucks, only one instance is allowed sorry!!!!");
_current = value;
}
}
}
Any my tests around this:
public class UnitTest1
{
private readonly TheStaticFoo foo;
public UnitTest1() => foo = new TheStaticFoo();
[Fact]
public void Test1() => Assert.Equal(TheStaticFoo.Current, foo);
}
This runs perfectly fine.
But adding a second test fails as expected:
[Fact]
public void AddingThisFails() => Assert.Equal(TheStaticFoo.Current, foo);
This is because of the static not getting torn down between the test runs.
Is it possible to run the two tests in isolation without modifying TheStaticFoo? Here I am using xunit but this would apply hopefully to any other test framework.
I am not sure whether the AssemblyLoadContext is the way to go for this.