From b36dff67cefd5704bf1051e5b97f456e1d18c005 Mon Sep 17 00:00:00 2001 From: Terje Sandstrom Date: Sun, 18 Oct 2026 21:06:47 +0200 Subject: [PATCH] Add DependsOnFixture example to dependent tests guide Adds a three-fixture dependency chain snippet and explains ordering, skipping, AllowFailure and the same-namespace requirement. Co-Authored-By: Claude Opus 5.5 --- .../nunit/writing-tests/dependent-tests.md | 15 ++++++ .../DependentFixtureExamples.cs | 53 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 docs/snippets/Snippets.NUnit/DependentFixtureExamples.cs diff --git a/docs/articles/nunit/writing-tests/dependent-tests.md b/docs/articles/nunit/writing-tests/dependent-tests.md index 7029992f6..e5a4d4581 100644 --- a/docs/articles/nunit/writing-tests/dependent-tests.md +++ b/docs/articles/nunit/writing-tests/dependent-tests.md @@ -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 diff --git a/docs/snippets/Snippets.NUnit/DependentFixtureExamples.cs b/docs/snippets/Snippets.NUnit/DependentFixtureExamples.cs new file mode 100644 index 000000000..b230f7572 --- /dev/null +++ b/docs/snippets/Snippets.NUnit/DependentFixtureExamples.cs @@ -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 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