Skip to main content

aweXpect.Snapshots

Nuget

aweXpect.Snapshots compares strings and objects with reviewed snapshots, which are stored next to the tests or in a store of your own, e.g. to verify generated text or a complex result without writing out the expected value.

At a glance​

// compare a string with the snapshot that is stored next to the test
await Expect.That(description).MatchesSnapshot();

// compare an object, written completely and indented by the aweXpect formatter
await Expect.That(album).MatchesSnapshot();

// replace values that change from run to run before comparing
await Expect.That(playlist).MatchesSnapshot().Masking.Regex(@"plays=\d+", "plays=<count>");

// replace every Guid and ISO 8601 date with a numbered placeholder ({Guid_1}, {Date_1}, …)
await Expect.That(playlist).MatchesSnapshot().Masking.Guids().Masking.Dates();

// leave out or mask members of an object by their name or type
await Expect.That(album).MatchesSnapshot().Ignoring.Member("PlayCount").Masking.Member("Id");

// keep a snapshot per target framework and operating system (AlbumTests.Describes@net8.0@windows.snapshot.txt)
await Expect.That(exception.Message).MatchesSnapshot().UniqueFor.TargetFramework().UniqueFor.OperatingSystem();

The snapshot is stored as <Class>.<Method>.snapshot.txt next to the test file, where <Class> is the name of the test file. A missing or different snapshot fails the test and writes the actual value to <Class>.<Method>.snapshot.pending.txt; rename it to <Class>.<Method>.snapshot.txt to accept it, or set the environment variable AWEXPECT_ACCEPT_SNAPSHOTS to accept all changed snapshots of a test run. OrphanedSnapshots.CheckAsync() in the teardown of the test run reports the snapshots that no test uses any more on a build server, and deletes them while accepting changes.

Overview​

ExpectationSubjectSummary
MatchesSnapshotstringmatches the snapshot stored next to the test
MatchesSnapshotany Tmatches the snapshot of the object, written by aweXpect

Documentation​

The full documentation is available at docs.testably.org.