Lesson 17 of 93 · 8 min read · Updated
Snapshot Testing in .NET: From a Hand-Made Approval Test to Verify with Scrubbing
On this page
Some outputs are too large for individual assertions: a rendered invoice, a serialized API response, a generated report. Writing thirty Assert.Equal lines for them is slow and fragile. A snapshot test saves the approved output to a file once and compares every later run against it. This lesson builds the mechanism by hand first, so you see exactly how it works, and then does the same with the Verify library, which automates the workflow and cleans up values that change on every run.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3 (with xunit.runner.visualstudio and Microsoft.NET.Test.Sdk)
- Verify.Xunit 31.12.5
The Idea and the Code Under Test
A snapshot test has three steps:
- Produce the output and serialize it to text.
- Compare it with an approved reference file, named
*.verified.txt, that lives in source control. - If they differ, fail. The new output only passes after a person reviews it and approves it on purpose.
The candidate here is an invoice renderer. It produces multi-line text with aligned columns (FATURA means "invoice" and TOPLAM means "total").
M02.XUnitBasics/InvoiceRenderer.cs
Note the comment about culture. Number formatting such as N2 uses the current culture unless you pass one. On a machine with an English culture, 1.500,00 would become 1,500.00 and the snapshot would fail even though nothing is wrong. Anything that varies by machine, such as culture, time zone, line endings or machine name, must be fixed in the code or removed before comparison.
A Hand-Made Snapshot Test
M02.XUnitBasics.Tests/SnapshotTests.cs
The helper reads the approved file from the output folder. The test project copies it there with <None Update="Snapshots\*.verified.txt" CopyToOutputDirectory="PreserveNewest" />. When the file is missing, it writes the actual output next to it as Invoice.received.txt and fails with "approved snapshot not found, review the output and approve it". The approved file looks like this:
M02.XUnitBasics.Tests/Snapshots/Invoice.verified.txt
To see the cycle, delete the verified file, run the tests, inspect the received file, and copy it back as the approved version. Run these commands from the test project folder:
The Same Test with Verify
Verify automates all of this. Add Verify.Xunit and the static using <Using Include="VerifyXunit.Verifier" Static="true" />, and the whole helper becomes one line:
M02.XUnitBasics.Tests/VerifySnapshotTests.cs
The test returns the Task from Verify. On the first run, Verify writes VerifySnapshotTests.Render_InvoiceText_MatchesVerifiedSnapshot.received.txt into the source folder (not bin) and fails. You review the content and accept it, either by renaming it to .verified.txt or with the "accept" button of a diff tool. On later runs, any difference fails the test and Verify can open a diff tool that shows exactly what changed. The file name comes from the class and method names, so you never name snapshot files by hand.
Object Graphs and Scrubbing
Verify is not limited to strings. Pass an object, and it serializes it to readable, JSON-like text. Values that change on every run are replaced with stable placeholders, which is called scrubbing.
M02.XUnitBasics.Tests/VerifySnapshotTests.cs
M02.XUnitBasics.Tests/Snapshots/Verify/VerifySnapshotTests.ObjectGraph_IsSerialized_AndVolatileValuesAreScrubbed.verified.txt
The Guid and DateTime became Guid_1 and DateTime_1. If the same value appeared twice, both places would show the same placeholder, so the snapshot still proves that two fields hold the same id. Automatic scrubbing only works for typed values, though. A GUID stored in a string, or the name of the build machine, needs explicit rules:
M02.XUnitBasics.Tests/VerifySnapshotTests.cs
M02.XUnitBasics.Tests/Snapshots/Verify/VerifySnapshotTests.CustomScrubbing_RemovesMembersAndMachineSpecificValues.verified.txt
Project Setup and the Golden Rule
A few settings keep snapshot tests stable across a team:
*.verified.*files go into source control.*.received.*files are listed in.gitignoreand never get committed..gitattributespins line endings for verified files, so Windows and macOS checkouts produce identical files.- A module initializer turns off automatic diff tool launching, which is useful in a classroom or on CI. Comment that line out locally if you want the diff tool to open.
.gitattributes
M02.XUnitBasics.Tests/VerifySnapshotTests.cs
The golden rule is the same for the hand-made version and for Verify: never approve a snapshot blindly. Approving a snapshot is a code review. If you rename every received file to verified without reading it, the test will happily lock in a bug. Read the diff, ask whether each change is intended, and only then accept it. Also keep snapshots small and focused. A snapshot of an entire page or a huge object fails for every unrelated change, and people stop reading those diffs.
Summary
- A snapshot test compares output with an approved
*.verified.txtfile and fails until a person approves any change. - Remove anything that varies by machine or run: pass a culture explicitly, fix dates, and normalize line endings.
- Verify replaces the hand-made helper with one call,
Verify(value), and names files after the test class and method. - Verify serializes objects and scrubs
GuidandDateTimevalues. UseIgnoreMemberandScrubMachineNamefor values it cannot detect. - Commit verified files, ignore received files, and treat every approval as a code review.