Getting started
editInstall the package
Add the Meziantou.Framework.InlineSnapshotTesting package to a test project:
dotnet package add Meziantou.Framework.InlineSnapshotTestingThe examples of this documentation omit the using directives: they need using Meziantou.Framework.InlineSnapshotTesting;.
Write a test
Call InlineSnapshot.Validate with the value to check. You do not need to write the expected value:
var data = new
{
FirstName = "Gérald",
LastName = "Barré",
NickName = "meziantou",
};
// No need to write the expected value
InlineSnapshot.Validate(data);Run the test
The test fails: the serialized value does not match the missing snapshot. The failure message shows the difference between the expected and the actual value.
By default, the library never changes your source code (the update strategy is Disallow): copy the actual value into the test yourself, or let the library do it. To have a merge tool open on each new or changed snapshot, set the update strategy, for instance in a module initializer (see Settings):
InlineSnapshotSettings.Default = InlineSnapshotSettings.Default with
{
SnapshotUpdateStrategy = SnapshotUpdateStrategy.MergeTool,
};Accept the change in the merge tool, or use the Overwrite strategy to write it directly. Either way, the source code of the test becomes:
var data = new
{
FirstName = "Gérald",
LastName = "Barré",
NickName = "meziantou",
};
InlineSnapshot.Validate(data, """
FirstName: Gérald
LastName: Barré
NickName: meziantou
""");Run the test again: it passes. From now on, it fails whenever the serialized value differs from the string.
Requirements
The library edits the source file of the test, so the tests must run on the machine that has the source code, from a build of that code with its PDB files, typically a Debug build. It finds the call to update from [CallerFilePath] and [CallerLineNumber] and checks them against the PDB. See String formats and source files.
Next steps
- Settings: configure every test of a project in one place.
- Serializers: control how values are written.
- Updating snapshots: choose how and when snapshots are updated.