Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/articles/nunit/writing-tests/dependent-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ When a whole class of tests depends on another class, put [`[DependsOnFixture]`]
the class, with the type of the fixture it depends on. It works the same way: the dependent fixture waits for the other
fixture, and is skipped if that fixture fails, unless `AllowFailure` is set.

[!code-csharp[DependentFixtures](~/snippets/Snippets.NUnit/DependentFixtureExamples.cs#DependentFixtures)]

- `SeedDatabaseTests` has no dependency, so it runs first and fills the shared `CustomerDatabase`.
- `CustomerReportTests` starts only after **all** tests in `SeedDatabaseTests` have finished. If any of them fails, the
fixture fails, and **every** test in `CustomerReportTests` is skipped, with a message that names the failed
dependency.
- `CleanUpDatabaseTests` depends on `CustomerReportTests` with `AllowFailure = true`. It runs last, even if a report
test failed, so the shared data is always removed.
- Dependencies chain: `CleanUpDatabaseTests` waits for `CustomerReportTests`, which waits for `SeedDatabaseTests`.
When you run only a dependent fixture, for example from your IDE, NUnit still includes the fixture it depends on.

A fixture has at most one `[DependsOnFixture]`, and the fixture it names must be in the same namespace. A fixture in
another namespace, or one that was not found in the run, makes the dependent fixture fail as invalid. A fixture can't
depend on its own base class.

## Things to keep in mind

- Tests in a dependency chain can't run in parallel with each other. Don't mark them
Expand Down
53 changes: 53 additions & 0 deletions docs/snippets/Snippets.NUnit/DependentFixtureExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using NUnit.Framework;

// Fixture dependencies are resolved between fixtures in the same namespace,
// so these examples have their own namespace.
namespace Snippets.NUnit.DependentFixtures;

#region DependentFixtures
// State shared between fixtures. Fixture dependencies make the order safe.
public static class CustomerDatabase
{
public static List<string> Customers { get; } = [];
}

public class SeedDatabaseTests
{
[Test]
public void AddCustomers()
{
CustomerDatabase.Customers.AddRange(["Alice", "Bob"]);
Assert.That(CustomerDatabase.Customers, Has.Count.EqualTo(2));
}
}

[DependsOnFixture(typeof(SeedDatabaseTests))]
public class CustomerReportTests
{
// All tests in this fixture run after every test in SeedDatabaseTests.
// If SeedDatabaseTests fails, this whole fixture is skipped.
[Test]
public void Report_ListsAllCustomers()
{
Assert.That(CustomerDatabase.Customers, Is.EquivalentTo(new[] { "Alice", "Bob" }));
}

[Test]
public void Report_HasNoDuplicates()
{
Assert.That(CustomerDatabase.Customers, Is.Unique);
}
}

[DependsOnFixture(typeof(CustomerReportTests), AllowFailure = true)]
public class CleanUpDatabaseTests
{
// Runs after CustomerReportTests even if it failed, so the cleanup always happens.
[Test]
public void RemoveCustomers()
{
CustomerDatabase.Customers.Clear();
Assert.That(CustomerDatabase.Customers, Is.Empty);
}
}
#endregion
Loading