xUnit.net Global setup teardown?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
xUnit.net does not have a single magic hook called "global setup" in the way some older test frameworks do. Instead, it gives you a few fixture patterns that match different scopes, so the right answer depends on whether you need per-test, per-class, per-collection, or assembly-wide initialization.
The xUnit Lifecycle Model
The first thing to understand is that xUnit encourages test isolation. A new instance of the test class is created for each test, so constructor logic acts like per-test setup and Dispose acts like per-test teardown.
That pattern is the default and should be your first choice when setup is cheap.
Sharing State Across Tests with Fixtures
When setup is expensive, such as starting a container or creating a database, xUnit fixtures let you share that work.
Class Fixtures
A class fixture is created once for a test class and shared by all tests in that class.
Use this when one test class owns the shared setup.
Collection Fixtures
A collection fixture is shared across multiple test classes. That is the closest option in xUnit v2 to "global setup" for a related group of tests.
This keeps setup centralized while still limiting the scope to a known set of tests.
Assembly-Wide Setup
Assembly fixtures were introduced in xUnit.net v3. If you are on v3, you can share one fixture across the entire test assembly.
This is true assembly-wide setup and teardown, but it is version-specific. If you are using xUnit v2, this exact feature is not available.
Choosing the Right Scope
Pick the smallest scope that solves the problem. If you only need a fresh object per test, use the constructor and Dispose. If an expensive shared dependency belongs to one class, use IClassFixture. If several classes need the same dependency, use ICollectionFixture. Reserve assembly-wide state for resources that are genuinely global and safe to share.
Smaller scopes reduce coupling and make parallel test execution safer. Large shared fixtures can speed tests up, but they also make isolation failures harder to debug.
Common Pitfalls
- Looking for a
[SetUp]or[TearDown]attribute because another framework uses that style. xUnit is intentionally different. - Assuming there is one built-in global setup API in every xUnit version. Assembly fixtures are an xUnit v3 feature.
- Sharing mutable state through fixtures and then getting flaky tests when execution order changes.
- Doing too much work in the test class constructor when the work should really be shared in a fixture.
- Forgetting cleanup in
Dispose, especially for files, sockets, containers, and database connections.
Summary
- xUnit uses constructors and
Disposefor per-test setup and teardown. - '
IClassFixtureshares setup across one test class.' - '
ICollectionFixtureshares setup across multiple related test classes.' - Assembly-wide fixtures exist in xUnit.net v3, not in every earlier version.
- Choose the smallest fixture scope that keeps tests isolated and predictable.
Related reading
- You must add a reference to assembly ''netstandard, Version2.0.0.0
- Your project is not referencing the .NETFramework,Versionv4.5 framework.
- A definitive guide to API-breaking changes in .NET
- A difference in style IDictionary vs Dictionary
- ZooKeeper session expired in tests
- 10 fold cross validation
- A fast array shift implementation in C?
- A fatal error occurred. The folder /usr/share/dotnet/host/fxr does not exist

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.