GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - WickedFlame/Polaroider: Automated Snapshottesting for .NET

BranchesTags
Open more actions menu

Latest commit

 

History

307 Commits
307 Commits

Folders and files

NameName
Last commit message
Last commit date
.nuke
.nuke
 
 
build
build
 
 
docs
docs
 
 
src
src
 
 
.gitignore
.gitignore
 
 
.gitlab-ci.yml
.gitlab-ci.yml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
Polaroider.lutconfig
Polaroider.lutconfig
 
 
Polaroider.sln
Polaroider.sln
 
 
README.md
README.md
 
 
appveyor.yml
appveyor.yml
 
 
build.cmd
build.cmd
 
 
build.ps1
build.ps1
 
 
build.sh
build.sh
 
 
testenvironments.json
testenvironments.json
 
 

Repository files navigation

Polaroider

Automated Snapshottesting for .NET

Build status Build status

NuGet Version NuGet Version

Maintainability Rating Coverage

Codacy Badge

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();

Testing with Snapshots

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.

Troubleshooting

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

Examples

[Test]
public void TestPerson()
{
    // act
    var person = repository.LoadTestPerson(...);
    // assert
    person.MatchSnapshot();
}

Update Snapshots

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.

Custom Snapshot Name

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();
}

Snapshot Metadata

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

Mocking changing data like DateTimes

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.

Update from v1 to v2

v1 was focused on simplicity. v2 is focused on flexibility while maintaining simplicity.

There are some breaking changes when updating to v2.

Mapping of simple types

  • 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();
});

About

Automated Snapshottesting for .NET

wickedflame.github.io/Polaroider/

Topics

approval-testingautomated-snapshottestingdotnetsnapshot-testingsnapshotssnapshottest

Resources

Readme
MIT license
Activity
Custom properties

Stars

7 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.