Lesson 23 of 93 · 6 min read · Updated
IClassFixture in xUnit: Sharing Expensive Setup Across Tests in One Class
On this page
xUnit creates a new instance of the test class for every test. That isolation is a good default, but it becomes a cost when the setup is slow, such as loading reference data or creating a database schema. IClassFixture<T> lets all tests in one class share a single, expensive object. This lesson shows how a class fixture is created, injected and disposed, and the one rule you must follow to keep shared state from breaking your tests.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0
Why xUnit Creates a New Instance per Test
In xUnit, the test class constructor is the setup code and Dispose is the teardown code. Because every test gets a fresh instance, fields set in the constructor never leak from one test into another. A test that changes a field cannot affect the next test.
The price is that the constructor runs once per test. If it takes 50 milliseconds and the class has 100 tests, the suite spends five seconds on setup alone. When the setup is a container start or a schema migration, the numbers get much worse. A class fixture solves this by moving the expensive work out of the constructor into an object that xUnit creates once for the whole class.
The Expensive Object
The example uses a class that stands in for slow setup. It loads a small table of city codes and then sleeps for 50 milliseconds to simulate real work. In a real project, this could be parsing a large file, starting a container or building a database.
M03.DataDriven/ReferenceDataStore.cs
The class implements IDisposable. xUnit calls Dispose on a fixture when it is done with it, so this is where you would stop a container or delete a temporary database.
InstanceCount is a static field. Static mutable state is usually something to avoid in tests, and that is still true. Here it is only a teaching aid: it counts how many times the object was created, which lets you prove the fixture is shared. Do not copy this pattern into real fixtures.
Using IClassFixture
To share the store, the test class implements IClassFixture<ReferenceDataStore> and takes the fixture as a constructor parameter.
M03.DataDriven.Tests/ClassFixtureTests.cs
IClassFixture<T> is a marker interface. It has no members to implement. It tells xUnit: "before running the tests in this class, create one T, and pass it to every instance of the class."
The test class itself is still created once per test. Three tests mean three instances of ClassFixtureTests. All three receive the same ReferenceDataStore, so the 50 millisecond setup runs once instead of three times.
The Fixture Lifecycle
xUnit manages the fixture for you:
- Before the first test in the class runs, xUnit creates the fixture by calling its constructor.
- For each test, xUnit creates a new test class instance and passes the same fixture to its constructor.
- After the last test in the class finishes, xUnit calls
Disposeon the fixture if it implementsIDisposable.
The test Fixture_IsNotDisposedWhileTestsRun checks step 3 from the inside: while tests are still running, the fixture has not been disposed yet.
If the fixture needs asynchronous setup or teardown, for example starting a container, implement IAsyncLifetime on the fixture. xUnit then awaits InitializeAsync before the first test and DisposeAsync after the last one.
xUnit creates the fixture itself, not a dependency injection container. Apart from a few framework types such as IMessageSink, the fixture constructor takes no parameters. If the fixture needs configuration, build or read it inside the fixture constructor.
Pitfall: Shared Mutable State
A class fixture is shared state by design. If one test changes it, every later test in the class sees the change. Tests start to depend on the order in which they run, and a test that passes alone fails when run with the others.
Follow one of these two rules:
- Keep the fixture read-only.
ReferenceDataStoreonly exposes a lookup method and properties with private setters. Tests can read from it but cannot change its data. - Reset it in each test. If the fixture must hold state, such as a database, the test class constructor cleans or resets that state before each test. Tools like Respawn exist for exactly this purpose with databases.
Also keep in mind that the fixture is shared only within one class. Another test class that implements IClassFixture<ReferenceDataStore> gets its own instance. Sharing one instance across several classes needs a collection fixture, which the next lesson covers.
Summary
- xUnit creates a new test class instance per test, so expensive constructor setup runs every time.
IClassFixture<T>creates oneTfor all tests in a class and injects it through the constructor.- The fixture is created before the first test and disposed after the last test of the class.
- Use
IAsyncLifetimeon the fixture for asynchronous setup and teardown. - Keep the fixture read-only, or reset its state in each test, so tests do not affect each other.