When snapshot testing with Verify beats explicit assertions, how .verified and .received files, scrubbing and accepting changes work, and how to keep snapshots reviewable and maintainable.
Use snapshot testing when the output is large or structured and you care about all of it: an API response, a generated email, source generator output, an OpenAPI document. Verify serialises the result to a .verified.txt file that is committed with the code. On the next run it compares the new output and, if anything differs, writes a .received.txt file, fails the test and shows a diff.
Verify scrubs volatile values such as Guids and dates by default. When a change is intended, you review the diff and accept it, and the updated snapshot is reviewed again in the pull request.
I keep explicit assertions for the one or two values that are the business rule, such as "GST is 10%". The trade-off is that snapshots replace a lot of assertion code, but if people accept them without reading, they only detect change. They stop checking correctness.
"Snapshot testing, or approval testing, means: run the code, serialise the whole result, and compare it with a file a human approved earlier. In .NET the tool is Verify. The test ends with
return Verify(result);. The first run writes a.received.txtfile and fails. I look at it, accept it, and it becomesClassName.MethodName.verified.txt, which I commit. On later runs any difference fails the test, writes a new received file and opens my diff tool.I reach for it when the output is big or shaped like a document: a JSON response with twenty fields, a rendered email, the OpenAPI document, the C# a source generator emits. Writing twenty
Assert.Equallines is tedious, and people skip the fields that matter. With Verify the whole thing is checked, and the diff in the pull request shows the reviewer exactly what changed in the contract. Values that change every run get scrubbed. Verify replaces Guids and dates withGuid_1andDateTime_1by default, and I addScrubMemberfor things like an ETag.The mistake I see is treating snapshots as a box to tick: the test fails, someone runs accept-all and commits it without reading. Then the snapshot only tells you that something changed, not whether it's right. So I keep explicit assertions for the values that encode a rule, like 'total includes 10% GST' or 'status is 409', and use snapshots for the shape around them. Snapshots show you what changed. Assertions say what must stay true."
| Use a snapshot when | Use explicit assertions when |
|---|---|
| Output is large or nested (JSON, HTML, emails) | One or two values carry the meaning |
| You care about the whole contract shape | The rule should be readable in the test ("10% GST") |
| Output is generated (OpenAPI, source generators, SQL from EF Core) | Output is non-deterministic and hard to scrub |
| You want reviewers to see contract changes in the PR diff | The team won't review snapshot diffs |
| Task | How |
|---|---|
| Write a snapshot test | return Verify(result); (or VerifyJson, VerifyXml) |
| Scrub a volatile member | .ScrubMember("ETag"), or .ScrubLinesContaining("traceId") |
| Accept changes | Diff tool "accept", or dotnet verify accept (the Verify.Tool global tool) |
| Keep CI clean | Commit *.verified.*, add *.received.* to .gitignore. On CI, Verify detects the build server and doesn't launch diff tools. |
| Test HTTP or EF Core | The Verify.Http and Verify.EntityFramework extensions |
A snapshot comparison is one serialisation plus a file read, so it costs about the same as a handful of assertions. The real cost is maintenance. Every intended change to the output means reviewing a diff, which is a good thing for public contracts and a nuisance for internal noise. Keep snapshots focused: snapshot the DTO, not the whole HttpResponseMessage with every header.
A public API with around 40 endpoints can protect its contract with one Verify test per endpoint, plus a snapshot of the OpenAPI document. Then a renamed property shows up as a one-line diff in review, not as a broken mobile client after release.
Checks whether the candidate knows how snapshot testing works and can judge when it helps and when it only detects change.
Green flags
Red flags
A normal assertion encodes the expected value in the test. An approval test stores it outside the test, as a file a human approved. The test becomes "produce the output, compare it with what was approved".
first run: result ββserialiseβββΆ Class.Method.received.txt ββ(human accepts)βββΆ Class.Method.verified.txt
later runs: result ββserialiseβββΆ compare with .verified.txt
ββ same β pass
ββ different β write .received.txt, fail, launch diff tool
DateTime, DateTimeOffset and similar values with numbered placeholders (Guid_1, DateTime_1). The same Guid gets the same placeholder, so you can still see that two fields hold the same id. Extra scrubbers include ScrubMember, ScrubLinesContaining and AddScrubber..verified.txt change. The reviewer sees the contract change line by line.Add a .gitattributes rule such as *.verified.txt text eol=lf so Windows and Linux agents don't fail on line endings.
Microsoft.AspNetCore.OpenApi.ToQueryString() output, so an innocent LINQ change that turns a join into N+1 shows up in review.ToQueryString() for an EF Core query, then add an Include and compare the SQL.*.received.* to .gitignore and run the suite with a deliberately failing snapshot on CI.Snapshot the shape of a quote, but assert the GST rule explicitly (xUnit v3, Verify.XunitV3).
#nullable enable
using Xunit;
using static VerifyXunit.Verifier;
namespace Quotes.Tests;
// ----- Code under test -----
public sealed record QuoteLine(string Sku, int Quantity, decimal UnitPrice);
public sealed record Quote(
Guid Id,
DateTimeOffset CreatedAt,
IReadOnlyList<QuoteLine> Lines,
decimal Subtotal,
decimal Gst,
decimal Total);
public static class QuoteCalculator
{
private const decimal GstRate = 0.10m; // Australian GST
public static Quote Create(IReadOnlyList<QuoteLine> lines, TimeProvider clock)
{
var subtotal = lines.Sum(l => l.Quantity * l.UnitPrice);
var gst = Math.Round(subtotal * GstRate, 2, MidpointRounding.AwayFromZero);
return new Quote(Guid.NewGuid(), clock.GetUtcNow(), lines, subtotal, gst, subtotal + gst);
}
}
// ----- Tests -----
public sealed class QuoteCalculatorTests
{
private static readonly QuoteLine[] Lines =
[
new("WIDGET", 3, 19.95m),
new("GADGET", 1, 120.00m)
];
// Explicit assertion: the business rule is readable in the test.
[Fact]
public void Gst_is_ten_percent_of_subtotal()
{
var quote = QuoteCalculator.Create(Lines, TimeProvider.System);
Assert.Equal(179.85m, quote.Subtotal);
Assert.Equal(17.99m, quote.Gst); // 17.985 rounds away from zero
}
// Snapshot: the whole shape, so any added, removed or renamed field shows up in review.
// Id and CreatedAt are scrubbed automatically to Guid_1 and DateTimeOffset_1.
[Fact]
public Task Quote_shape_is_stable()
{
var quote = QuoteCalculator.Create(Lines, TimeProvider.System);
return Verify(quote);
}
}
The committed QuoteCalculatorTests.Quote_shape_is_stable.verified.txt:
{
Id: Guid_1,
CreatedAt: DateTimeOffset_1,
Lines: [
{ Sku: WIDGET, Quantity: 3, UnitPrice: 19.95 },
{ Sku: GADGET, Quantity: 1, UnitPrice: 120.00 }
],
Subtotal: 179.85,
Gst: 17.99,
Total: 197.84
}
# .gitignore: never commit received files
echo "*.received.*" >> .gitignore
# Accept all pending snapshots after reviewing the diffs
dotnet tool install --global verify.tool
dotnet verify accept
.verified changes in the pull request.HttpResponseMessage, including headers, timings and trace ids. Why it is wrong: Unrelated changes fail the test, so people start accepting blindly. Instead: Snapshot the DTO or body, and scrub what remains volatile..received.txt files, or not committing .verified.txt. Why it is wrong: CI either fails for everyone or has nothing to compare with. Instead: Commit verified files and gitignore received files.*.verified.txt text eol=lf to .gitattributes.Snapshot tests are strongest at boundaries: API contracts, messages other teams consume, generated code. There, the important question is "did the contract change?", and a diff in the pull request answers it better than any assertion. Inside the domain I want readable, explicit assertions that name the rule. I check the balance by looking at how often snapshot updates get merged with no reviewer comment. If that's nearly always, the snapshots are too broad.
Verify is MIT-licensed and actively maintained, so there's no licence risk, unlike FluentAssertions, which has needed a commercial licence since version 8 in 2025.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Verify snapshot | Large or structured output, public contracts, generated code | Diff review on every intended change | Output you can't make deterministic |
| Explicit assertions | One or two meaningful values | More code for large objects | Asserting 30 fields one by one |
| Snapshot + key assertions | Contract shape plus a business rule | Both, but small | Never a bad default for important endpoints |
| Contract tests (Pact) | Agreement between separate teams or services | Broker and pipeline setup | A single team owns both sides |
Last updated 23 Nov 2026.