Automated Snapshottesting for .NET
Simplify UnitTesting with snapshots.
Polaroider is a Approval Testing Framework that creates and compares snapshots of objects.
This makes testing of objects and their content easy and fast.
// arrange var repository = new PersonRepository(); // act var person = repository.LoadTestPerson(...); // assert person.MatchSnapshot();
Snashots help when testing objects or large strings
- Snapshots ensure that object structure does not change unintendedly. When adding or removing properties in objects, these are automatically captured and asserted.
- Sanpshots ensure the state of objects. Allways ensure the atomic state of obejcts. Any changes to the data contained in the properties of the matched object is automatically validated and asserted.
- Simplify comparing big strings. Just create a snapshot of the string and ensure it does not change in any future testrun.
Visit https://wickedflame.github.io/Polaroider/ for the full documentation.
When having trouble generating Snapshots or the TestMethodNotFoundException is thrown, please make sure that the option for Optimize code is disabled *.pdb files are generated and that the Test does not depend on async/await. For more information visit https://wickedflame.github.io/Polaroider/troubleshooting
[Test] public void TestPerson() { // act var person = repository.LoadTestPerson(...); // assert person.MatchSnapshot(); }
Data changes and the snapshot has to be updated. This can be done by adding the UpdateSnapshot attribute to the test method or class.
[Test] [UpdateSnapshot] public void TestPerson() { // act var person = repository.LoadTestPerson(...); // assert person.MatchSnapshot(); }
It is important to remove the attribute after the snapshot is updated. Else the snapshot will be updated in every test run and the test will not fail when the data changes.
Usig the SnapshotName attribute you can specify the name of the snapshot for a test method. This is useful to avoid long filenames of snapshots.
[SnapshotName("CustomSnapshotName")] public void TestPerson() { // act var person = repository.LoadTestPerson(...); // assert person.MatchSnapshot(); }
Metadata allows storing multiple snapshots for a single test. Each snapshot is identified by the metadata passed to MatchSnapshot. This is useful when a test asserts several variations of an object and each variation should be matched against its own snapshot.
[Test] public void TestPerson() { var person = repository.LoadTestPerson(...); // match against the snapshot that has the metadata id = 1 person.MatchSnapshot(() => new { id = 1 }); }
The metadata is written as a header into the snapshot file so multiple snapshots can be stored side by side.
---metadata
id: 1
---data
data: this is a test
value: 1
Some values, like DateTime or Guid, change with every test run and would make a snapshot fail even when nothing else changed. These values can be mocked to a constant so the snapshot stays stable.
[Test] public void TestPerson() { var person = repository.LoadTestPerson(...); // mock all DateTimes to a constant value person.MatchSnapshot(o => o.MockDateTimes()); }
The builtin extensions MockDateTimes() and MockGuids() replace all occurrences with a constant value (e.g. 0000-00-00T00:00:00.0000). For custom values a formatter can be registered instead.
var options = SnapshotOptions.Create(o => o.AddFormatter<DateTime>(obj => "0000-00-00T00:00:00.0000")); person.MatchSnapshot(options);
See Mocking data for more details.
v1 was focused on simplicity. v2 is focused on flexibility while maintaining simplicity.
There are some breaking changes when updating to v2.
- DateTimes are mapped in the ISO 8601 format to be Culture-Independent
- Null strings are now displayed as null
- Empty strings are now displayed as ''
Before updating to v2 ensure that all tests are executed without errors. If some tests throw a MismatchException there are two possibilities:
- Update the snapshots with the UpdateSnapshot Attribute
- Set the UseBasicFormatters on the SnapshotOptions. This resets all Formatters to the same as were used in v1
SnapshotOptions.Setup(o => { o.UseBasicFormatters(); });