NUnit C# Samples ๐งช
A collection of practical, ready-to-run NUnit examples in C# โ great for learning NUnit, exploring its features, or grabbing a snippet when you need one.
All projects target .NET 10 and use the latest NUnit packages.
๐ Projects
๐ฐ money
A classic example straight from the NUnit heritage: a Money and MoneyBag implementation with a full set of tests. A great starting point for seeing how NUnit tests are structured around a real domain model.
โ
Syntax
Side-by-side examples of NUnit's constraint-based syntax (Assert.That) and the classic syntax (Assert.AreEqual, StringAssert, CollectionAssert, etc.), so you can see both styles and pick what works for you.
The examples are split into focused fixtures by topic:
| File |
What it covers |
SimpleConstraintTests |
Null checks, Is.True/False, Is.NaN, Is.Empty |
TypeConstraintTests |
Is.TypeOf, Is.InstanceOf, Is.AssignableFrom |
StringConstraintTests |
Does.Contain, Does.StartWith, Does.EndWith, IgnoreCase, regex |
EqualityTests |
Is.EqualTo, tolerance for floats, decimals and mixed numeric types |
ComparisonTests |
Is.GreaterThan, Is.LessThan and their inclusive variants |
CollectionTests |
Has.All, Has.Some, Has.None, equivalence, subset |
PropertyTests |
Has.Property, Has.Length, Has.Count |
ConstraintOperatorTests |
Is.Not, !, &, | operators and complex combinations |
AssumptionsAndWarningsTests |
Assume.That, Warn.If, Warn.Unless |
๐ DataDrivenTests
Shows how to drive tests from data rather than writing one test per case. Covers all the main approaches:
| File |
What it covers |
DataDrivenTestFixture |
[TestFixture(value)] โ parameterised fixtures with constructor arguments |
GenericTestFixture |
[TestFixture(typeof(T))] โ generic fixtures with type arguments |
TestCasesFixture |
[TestCase] โ inline data, ExpectedResult, TestName, Description |
TestCaseSourceFixture |
[TestCaseSource] โ static methods, fields, properties, TestCaseData, external source classes |
๐ฒ TestCaseGeneration
Shows how to generate test cases automatically from parameter-level attributes, so NUnit builds the combinations for you rather than you spelling out each case by hand.
The attributes fall into two independent dimensions:
Generation attributes โ define what values each parameter receives:
| File |
What it covers |
ValuesFixture |
[Values] โ inline values, bool and enum shorthands, null |
ValueSourceFixture |
[ValueSource] โ static field, method, property, external class |
RangeFixture |
[Range] โ int, double, float, descending, multi-parameter |
RandomFixture |
[Random] โ unbounded, bounded, double, multi-parameter |
Strategy attributes โ define how NUnit combines values across multiple parameters. All four fixtures below use identical parameters so the effect of the strategy is the only variable:
| File |
Strategy |
Tests generated |
StrategyCombinatorialFixture |
[Combinatorial] |
27 โ every combination (also the default) |
StrategySequentialFixture |
[Sequential] |
3 โ values paired by index |
StrategyPairwiseFixture |
[Pairwise] |
~9 โ minimum set covering all value pairs |
๐ ExpectedExceptionExample
An example of extending NUnit with a custom attribute. Shows how to implement a [ExpectedException] attribute โ something that existed in NUnit 2 but was removed in NUnit 3. Walking through it teaches you how NUnit's attribute and command pipeline works.
๐ก For everyday use, Assert.Throws<T>() is the right tool. This example is about understanding how to build your own test attributes, not about replacing Assert.Throws.
๐ TimeoutRetryAttributeExample
Another example of extending NUnit with a custom attribute. Shows how to implement a [TimeoutRetry] attribute that automatically retries a test โ but only when it times out, not when it fails for other reasons. Useful as a pattern for handling flaky tests caused by slow or unreliable network connections.
๐ก Three of the tests in this project are marked [Explicit] because they are intentionally designed to fail โ they demonstrate what happens when retries are exhausted, or when a failure is due to an assertion or exception rather than a timeout.
๐ Running the samples
Each project is a self-contained test project. To run all samples at once, just run from the repo root:
dotnet test
To run a single project, specify its folder:
dotnet test AssertSyntax
dotnet test DataDrivenTests
Or open the repo in Visual Studio or Rider and run the tests from the IDE's test explorer.