A minimal .NET 9 project demonstrating snapshot testing using Verify and NUnit.
Snapshot testing captures the output of your code and saves it as a verified snapshot file. Future test runs compare new output against these snapshots, making it easy to detect unexpected changes. It's particularly useful for testing complex object graphs where writing manual assertions would be tedious and error-prone.
snapshot-testing/
├── SnapshotDemo/ # Production code
│ ├── Person.cs # Person and Address records
│ ├── ValidationResult.cs # ValidationResult and ValidationMetadata records
│ └── PersonProcessor.cs # System under test
└── SnapshotDemo.Tests/
└── PersonProcessor/
└── ValidatePersonTests/
├── Tests.cs # Test cases
├── Tests.Basic.verified.txt
├── Tests.ExplicitScrubbing.verified.txt
└── Tests.TestThatIncludesInput.verified.txt
The Basic test shows the simplest use case - verify the entire output structure with minimal configuration.
Verify automatically scrubs (replaces with placeholders) certain types like Guid and DateTime to make tests deterministic. These appear as Guid_1, DateTime_1, etc. in snapshots.
The ExplicitScrubbing test demonstrates how to scrub specific properties (like Status) that aren't automatically scrubbed.
The TestThatIncludesInput test shows a powerful pattern: by including both input and output in the snapshot with Verify(new { input, output }), you can:
- Make snapshots more readable by showing the transformation
- Implicitly assert that certain values (like
PersonId) are preserved from input to output
When the same Guid appears in both input and output, Verify assigns it the same placeholder (e.g., Guid_2), effectively asserting equality without explicit code.
Both input (Person with nested Address) and output (ValidationResult with nested ValidationMetadata) demonstrate that snapshot testing handles complex object graphs effortlessly.
dotnet testThe first time you run a new test, Verify creates .received.txt files. Review these, and if correct, copy them to .verified.txt to approve the snapshot.
.verified.txtfiles are committed to Git and represent the approved baseline.received.txtfiles are generated during test runs and ignored by Git (via.gitignore)- Snapshots are co-located with test files for easy discovery
- .NET 9
- NUnit 4.4
- Verify.NUnit 31.0