Snapper 2.4.1

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Snapper --version 2.4.1
NuGet\Install-Package Snapper -Version 2.4.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Snapper" Version="2.4.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Snapper --version 2.4.1
#r "nuget: Snapper, 2.4.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Snapper as a Cake Addin
#addin nuget:?package=Snapper&version=2.4.1

// Install Snapper as a Cake Tool
#tool nuget:?package=Snapper&version=2.4.1

Snapper V2

Bringing Jest-esque Snapshot testing to C#

Build Status Nuget (with prereleases) license

Snapper is a NuGet library which makes snapshot testing very easy in C#. Snapshot testing can simplify testing and is super useful when paired with Golden Master testing or contract testing APIs. It is very heavily based on Jest Snapshot Testing framework.

See https://theramis.github.io/Snapper/ for the full documentation.

What is Snapper?

The best way to describe what Snapper does by going through an example. Imagine you have the following test where are retrieving a user from a service.

[Fact]
public void MyTest()
{
  var myUser = _userService.GetUser(id);
  Assert.Equal("MyUser", myUser.Username);
  Assert.Equal("email@example.com", myUser.Email);
  Assert.Equal("myhash", myUser.PasswordHash);
  ...
  ...
}

As you can imagine the assertion steps in the test can get very long and hard to read. Now lets see what the test would look like if Snapper is used.

[Fact]
public void MyTest()
{
  var myUser = _userService.GetUser(id);
  myUser.ShouldMatchSnapshot();
}

The test above is now asserting the myUser object with a snapshot stored on disk. Snapper helps you create this snapshot at the beginnging (see Quick Start).

This is the basis of snapshot testing. The idea being a baseline is first generated (in this case a json file which is our snapshot) and then everytime the test runs the output is compared to the snapshot. If the snapshot and the output from the tests don't match the test fails!

As you can see using Snapper the test is now much smaller and easier to read but that's not all. Using Snapper brings with it a lot more benefits!

Why use Snapper?

Benefits of using Snapper/snapshot testing vs traditional assertions

  • Much easier to read - It's quite common to have a large list of assertions which can be hard to read. Snapper makes your tests a lot shorter and easier to read!
  • Very difficult to miss properties to assert - It's hard to validate that all properties have are being asserted using traditional assertions. By using Snapper the whole object asserted which means all properties are always asserted, so there is no chance of missing properties!
  • Captures changes to the object being asserted - It's quite common to add new properties to our objects over time. e.g. Adding FirstName to the myUser object above. Using traditional assertions the test would still pass and it's easy to forget to update the test. Using Snapper the test would immediately fail since it's a change in the system and the developer should verify if the change was expected!
  • Much quicker to write tests - Writing all those assertions above can be time consuming. With Snapper a json file is generated with the object which the developer can quickly verify!

When to use Snapper?

Use cases where Snapper/snapshot testing really shines

  • Contract testing - Testing your contract has not changed is a major part of maintaining any library/API. Snapper is excellent for this! Using Snapper any changes to the contract would immediately fail a test which lets the developer know that they might be breaking a contract they didn't expect.
  • Asserting complex objects - Sometimes you have to assert complex objects which can be hard and time consuming to get right. Snapper makes this easy and quick.
  • Golden Master testing - Golden master testing is a technique where you capture the behaviour of a system. Snapper is perfect for this as you can easily assert the behaviour without the complex setup and assertions. Snapper would also fail as soon as the behaviour of the system changes

The use cases above are just some of the examples I've found where Snapper is super useful. Feel free to try them in other situation you think would be useful.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

<table> <tbody> <tr> <td align="center" valign="top" width="14.28%"><a href="https://github.com/fgather"><img src="https://avatars3.githubusercontent.com/u/614354?v=4?s=100" width="100px;" alt="Florian Gather"/><br /><sub><b>Florian Gather</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=fgather" title="Code">πŸ’»</a> <a href="#ideas-fgather" title="Ideas, Planning, & Feedback">πŸ€”</a></td> <td align="center" valign="top" width="14.28%"><a href="https://www.linkedin.com/in/tomasbruckner/"><img src="https://avatars2.githubusercontent.com/u/7334618?v=4?s=100" width="100px;" alt="Tomas Bruckner"/><br /><sub><b>Tomas Bruckner</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=tomasbruckner" title="Code">πŸ’»</a></td> <td align="center" valign="top" width="14.28%"><a href="https://visualon.de"><img src="https://avatars1.githubusercontent.com/u/1798109?v=4?s=100" width="100px;" alt="Michael Kriese"/><br /><sub><b>Michael Kriese</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=ViceIce" title="Code">πŸ’»</a> <a href="#ideas-ViceIce" title="Ideas, Planning, & Feedback">πŸ€”</a> <a href="https://github.com/theramis/Snapper/issues?q=author%3AViceIce" title="Bug reports">πŸ›</a></td> <td align="center" valign="top" width="14.28%"><a href="http://cognitoforms.com"><img src="https://avatars0.githubusercontent.com/u/4603206?v=4?s=100" width="100px;" alt="Taylor Kimmett"/><br /><sub><b>Taylor Kimmett</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=tskimmett" title="Code">πŸ’»</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/PatrickLehnerXI"><img src="https://avatars1.githubusercontent.com/u/19566691?v=4?s=100" width="100px;" alt="Patrick Lehner"/><br /><sub><b>Patrick Lehner</b></sub></a><br /><a href="https://github.com/theramis/Snapper/issues?q=author%3APatrickLehnerXI" title="Bug reports">πŸ›</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/plitwinski"><img src="https://avatars3.githubusercontent.com/u/25408297?v=4?s=100" width="100px;" alt="Piotr Litwinski"/><br /><sub><b>Piotr Litwinski</b></sub></a><br /><a href="https://github.com/theramis/Snapper/issues?q=author%3Aplitwinski" title="Bug reports">πŸ›</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/WarrenFerrell"><img src="https://avatars0.githubusercontent.com/u/8977001?v=4?s=100" width="100px;" alt="Warren Ferrell"/><br /><sub><b>Warren Ferrell</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=WarrenFerrell" title="Code">πŸ’»</a></td> </tr> <tr> <td align="center" valign="top" width="14.28%"><a href="https://github.com/lilasquared"><img src="https://avatars3.githubusercontent.com/u/3036779?v=4?s=100" width="100px;" alt="Aaron Roberts"/><br /><sub><b>Aaron Roberts</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=lilasquared" title="Code">πŸ’»</a> <a href="#ideas-lilasquared" title="Ideas, Planning, & Feedback">πŸ€”</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/jaytulk"><img src="https://avatars.githubusercontent.com/u/34781842?v=4?s=100" width="100px;" alt="jaytulk"/><br /><sub><b>jaytulk</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=jaytulk" title="Code">πŸ’»</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/albertomartinsanchez"><img src="https://avatars.githubusercontent.com/u/4971812?v=4?s=100" width="100px;" alt="Alberto MartΓ­n"/><br /><sub><b>Alberto MartΓ­n</b></sub></a><br /><a href="https://github.com/theramis/Snapper/issues?q=author%3Aalbertomartinsanchez" title="Bug reports">πŸ›</a></td> <td align="center" valign="top" width="14.28%"><a href="https://github.com/ptjhuang"><img src="https://avatars.githubusercontent.com/u/5620750?v=4?s=100" width="100px;" alt="Peter Huang"/><br /><sub><b>Peter Huang</b></sub></a><br /><a href="https://github.com/theramis/Snapper/commits?author=ptjhuang" title="Code">πŸ’»</a></td> </tr> </tbody> </table>

This project follows the all-contributors specification. Contributions of any kind welcome!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Snapper:

Package Downloads
Snapper.Nunit

Bringing Jest-esque Snapshot testing to C#. See Project Site for more details

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Snapper:

Repository Stars
OmniSharp/csharp-language-server-protocol
Language Server Protocol in C#
Version Downloads Last updated
3.0.0-beta 123 1/2/2024
2.4.1 25,943 12/31/2023
2.4.0 146,823 12/11/2022
2.3.2 118,631 5/17/2022
2.3.1 136,543 12/6/2021
2.3.0 230,309 1/11/2021
2.2.4 189,831 3/16/2020
2.2.3 127,577 2/15/2020
2.2.2 22,315 2/1/2020
2.2.1 4,334 1/14/2020
2.2.0 86,974 11/1/2019
2.1.0 1,538 10/12/2019
2.0.1 5,486 9/11/2019
2.0.0 4,198 8/14/2019
2.0.0-beta3 24,712 5/25/2019
2.0.0-beta2 449 5/25/2019
2.0.0-beta 5,793 5/11/2019
2.0.0-alpha3 471 5/6/2019
2.0.0-alpha2 460 5/6/2019
2.0.0-alpha 536 4/23/2019