Lesson 83 of 93 · 5 min read · Updated
Organizing Integration Tests: Separate Projects and Shared Fixtures in xUnit
On this page
Integration tests have a different cost profile from unit tests. They start hosts, open database connections, run HTTP servers and sometimes download a browser. If they live in the same project as unit tests, every developer pays that cost on every run. This lesson shows how a Books API keeps its tests in separate projects, how one expensive WebApplicationFactory is shared across test classes with an xUnit collection fixture, and what that sharing costs you.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
Microsoft.AspNetCore.Mvc.Testing11.0.0Microsoft.Playwright.Xunit1.65.0 (only for the end-to-end project)- Docker is not required.
One Project per Cost Level
The solution has three projects for this API:
M09.IntegrationApi: the application itself.M09.IntegrationApi.Tests: integration tests withWebApplicationFactory, SQLite in-memory and WireMock.Net. 27 tests, about one second.M09.IntegrationApi.E2E.Tests: 3 end-to-end tests with Playwright and a real browser. The first run downloads Chromium (about 150 MB).
The split is not about naming. It lets you run each group on its own schedule. A developer runs the integration tests on every change, while a CI pipeline can run the end-to-end project in a separate, slower stage:
The E2E project explains its own existence in its project file:
M09.IntegrationApi.E2E.Tests/M09.IntegrationApi.E2E.Tests.csproj
The E2E test classes also carry a trait, [Trait("Kapsam", "E2E")], where Kapsam is the trait name meaning "scope". The trait lets you exclude them with a filter when you run the whole solution, as in the third command above. Separate projects and traits work together: the project boundary keeps dependencies such as Playwright out of the fast test project, and the trait gives you a filter when you run everything at once.
Shared Build Settings for Test Projects
A Directory.Build.props file at the root of the modules folder treats every project whose name ends in .Tests as a test project and adds the common packages. Each test .csproj then lists only what is specific to it.
Directory.Build.props
This convention matters more as the number of test projects grows. Adding a new integration test project is a new folder and a short .csproj, not a copy of package references that slowly drift apart.
Sharing the Expensive Fixture
Starting a WebApplicationFactory, opening the SQLite connection, creating the schema and starting a WireMock server takes time. Doing it once per test class would multiply that cost. xUnit's collection fixture creates one instance and shares it with every class in the collection:
M09.IntegrationApi.Tests/Infrastructure/ApiCollection.cs
Each test class joins the collection with an attribute and receives the factory through its constructor:
M09.IntegrationApi.Tests/HttpPipelineTests.cs
HttpPipelineTests, ExternalApiTests and BackgroundServiceIntegrationTests all use the same collection, so they share one host, one database and one WireMock server.
Pitfall: Shared Fixtures Mean Shared State
The comment on ApiCollection names the price. xUnit runs test classes in the same collection one after another, not in parallel, so the suite gets no speed-up from parallelism inside the collection. Worse, the data is shared. A book created by one test is still there when the next test starts, unless something removes it.
The tests handle this with a strict rule: every test resets the state it depends on at the start, in InitializeAsync. ResetDatabaseAsync deletes all rows. ExternalApiTests also calls CurrencyServer.Reset() to drop WireMock mappings and logs. The background service tests rely on the factory's FakeTimeProvider, which never moves unless a test calls Advance, so the hosted service cannot run in the middle of another test.
Reset at the start, not at the end. A test that fails halfway never reaches its cleanup code, and the next test would inherit a dirty state. Resetting first makes each test responsible for its own starting point.
Test classes that need no shared host should stay out of the collection. EfCoreProviderTests creates its own SQLite connection per test, and BackgroundServiceUnitTests builds the service by hand. Both run in parallel with the collection.
Summary
- Keep integration and end-to-end tests in their own projects so the fast tests stay fast and free of heavy dependencies.
- Add a trait to slow tests so you can exclude them with
--filterwhen running the whole solution. - Use a
Directory.Build.propsconvention to give every*.Testsproject the same test packages. - Share an expensive
WebApplicationFactorywithICollectionFixture<T>and[Collection]. - Shared fixtures run sequentially and share state: reset data, mocks and clocks at the start of every test.