Twelve xunit.v3 test classes each rebuild the same eight-second search index; how do you share one instance, and what does each option do to parallelism?
answer
- class scope means twelve builds
- [CollectionDefinition] + ICollectionFixture<T>
- [Collection("SearchIndex")] on each class
- one collection runs serially
- [assembly: AssemblyFixture(typeof(T))] stays parallel
basics
~10 sPut the classes in one test collection whose definition implements ICollectionFixture<SearchIndexFixture>: one index, but those classes run serially. Or register an assembly fixture: one index, classes stay parallel, so it must tolerate concurrent reads.
solid answer
~50 sA class fixture is scoped to one class, so twelve classes mean twelve builds. **Collection fixture**: declare `[CollectionDefinition("SearchIndex")] public class SearchIndexCollection : ICollectionFixture<SearchIndexFixture>;` in the test assembly and mark each class `[Collection("SearchIndex")]`; the type-safe `[Collection<SearchIndexCollection>]` needs an unnamed `[CollectionDefinition]`, because a named definition is matched by name only. One fixture is built before the collection's first test and disposed after its last, but a collection runs its tests serially, so those twelve classes stop running in parallel with each other. **Assembly fixture**, new in xunit.v3: `[assembly: AssemblyFixture(typeof(SearchIndexFixture))]` builds one instance before any test and disposes it after all of them; any class takes it through its constructor, and classes keep their own collections, so they still run in parallel and the fixture is read concurrently. Choose the collection when the shared object is mutable or not thread-safe, and the assembly fixture when it is effectively immutable.
code
csharp · 11 lines// Option 1: one collection; the classes run serially with each other
[CollectionDefinition] // unnamed: matched by type through [Collection<T>]
public class SearchIndexCollection : ICollectionFixture<SearchIndexFixture> { }
[Collection<SearchIndexCollection>]
public class RankingTests(SearchIndexFixture fixture)
{
[Fact]
public void Exact_title_ranks_first() =>
Assert.Equal("sku-1042", fixture.Index.Search("trail running shoe").First().Sku);
}go deeper
Know that collection and assembly fixtures exist for sharing one object across many test classes.
Explain how [CollectionDefinition], ICollectionFixture<T> and [Collection] fit together and that one collection runs its tests serially.
Choose between a collection fixture and an assembly fixture from the object's thread-safety and the parallelism you can afford, and compose scopes so the expensive part is shared and read-only.
Set suite-wide conventions for shared infrastructure: which resources may be assembly-wide, how their immutability is enforced, and how much parallelism the CI budget relies on.
## The problem with class scope A class fixture lives for one test class. When twelve classes, covering ranking, facets, typo tolerance and so on, each implement `IClassFixture<SearchIndexFixture>`, the eight-second index build runs twelve times. xunit.v3 offers two wider scopes, and they differ mainly in what they do to **parallelism**. ## Option 1: a collection fixture A **test collection** is xUnit.net's unit of parallel scheduling. By default each test class is its own collection; collections run in parallel with each other, and the tests inside one collection run one after another. 1. Declare a collection definition class in the test assembly: `[CollectionDefinition("SearchIndex")]` on a class implementing `ICollectionFixture<SearchIndexFixture>`. The class has no tests and no code; it exists only to carry the attribute and the interface. 2. Mark each of the twelve classes with `[Collection("SearchIndex")]`. The type-safe alternative, `[Collection<SearchIndexCollection>]`, avoids a mistyped name but only matches a definition declared **without** a name: xUnit.net keys a named definition by its name and an unnamed one by its type, so mixing the two styles silently leaves the classes outside the fixture's collection and their constructors fail with "did not have matching fixture data". 3. Take `SearchIndexFixture` in each class's constructor. The fixture is created before the first test in the collection and disposed after the last one. The cost is that the twelve classes are now **one collection**, so they run serially with respect to each other while other collections keep running in parallel. Placing `ICollectionFixture<T>` on a test class instead of the definition is rejected with "A test class may not be decorated with ICollectionFixture<>". xUnit.net reads collection definitions from the test assembly, so a definition kept in a shared helper library is not picked up. ## Option 2: an assembly fixture xunit.v3 adds a third scope. `[assembly: AssemblyFixture(typeof(SearchIndexFixture))]` registers a fixture that is created **before any test in the assembly** runs and cleaned up after all of them. Any test class gets it by declaring a constructor parameter of that type; no interface on the class is needed. The fixture must have a public parameterless constructor. The classes are untouched otherwise: each remains its own collection, so they keep running in parallel, and several tests may call `Search` on the same index at the same moment. That is fine for an index that is built once and only queried, and a bug for one that keeps a mutable cache or a non-thread-safe reader. ## Side by side | | Class fixture | Collection fixture | Assembly fixture | |---|---|---|---| | Declared with | `IClassFixture<T>` on the class | `[CollectionDefinition]` + `ICollectionFixture<T>`, `[Collection]` on classes | `[assembly: AssemblyFixture(typeof(T))]` | | Instances for twelve classes | twelve | one | one | | Effect on those classes' parallelism | none | they run serially with each other | none | | Concurrent access to the fixture | no (by default) | no | yes | | Available in | v2 and v3 | v2 and v3 | v3 | ## Combining scopes Scopes compose. A collection fixture's constructor may take assembly fixtures, and a class fixture's constructor may take collection and assembly fixtures. So the expensive index can be an assembly fixture, while a class fixture builds a cheap per-class `SearchSession` over it with its own query settings. Each class mutates only its own session, and the index stays shared and read-only. ## Choosing - The shared object is **immutable after construction** and safe for concurrent reads: an assembly fixture gives one build with no loss of parallelism. - It is **mutable, stateful or not thread-safe**, such as a single connection or a writable index: a collection fixture, accepting serial execution for its members. - Only one class needs it: stay with a class fixture. - A collection must not run alongside **anything** else, for example because it changes process-wide settings: add `DisableParallelization = true` to its `[CollectionDefinition]`. A quick check before choosing the assembly fixture: if any test would need to reset or modify the index, it is not read-only, and a collection fixture is the honest choice.
- How can each class tune query settings without mutating the shared index?Keep the index as an assembly or collection fixture and add a class fixture whose constructor takes it, for example `RankingSessionFixture(SearchIndexFixture shared)`, which builds a cheap per-class session with its own settings. Class fixtures may depend on collection and assembly fixtures, so the expensive part is built once and each class mutates only its own session.
- What does DisableParallelization = true on a [CollectionDefinition] change?It keeps that collection's tests from running in parallel with any other tests, so the collection runs on its own while the rest of the suite stays parallel. It suits a collection that changes process-wide state, such as environment variables or the current culture, which a plain shared collection would not protect against.
- A collection fixture is shared, yet its member classes keep running in parallel with each other.
- An assembly fixture forces every test in the assembly to run serially.
- ICollectionFixture<T> is declared directly on each test class that needs the fixture.
- An assembly fixture is safe for any mutable object because tests never overlap.
- A [CollectionDefinition] in a shared helper library is picked up by every test project.
- [Collection<SearchIndexCollection>] finds a definition declared as [CollectionDefinition("SearchIndex")].
Assumes xunit.v3 4.x; assembly fixtures ([assembly: AssemblyFixture]) and the generic [Collection<T>] attribute do not exist in xUnit.net v2, where a collection fixture was the only cross-class option.