Interview frequency: …
Short answer
xUnit creates a new test class instance for every [Fact] method to isolate state and prevent interference; it uses the constructor and IDisposable instead of Setup and Teardown.
Explanation
In NUnit, the same class instance is reused across tests unless configured otherwise, which may cause accidental state sharing. xUnit enforces strict isolation: it creates each test class with its constructor and disposes it through Dispose(). To share expensive data across tests, xUnit provides Fixtures through IClassFixture<T>.
Code example
public class CalculatorTests : IDisposable {
private readonly Calculator _calc = new(); // Clean and new for each Fact
[Fact]
public void AddsCorrectly() => Assert.Equal(4, _calc.Add(2, 2));
public void Dispose() { /* Clean up after every test */ }
}Common mistakes
- Relying on public class variables to share state between xUnit tests, not realizing each test has a separate instance.