Description
Snapshooter is a flexible snapshot testing tool to simplify the result validation in your unit tests in .Net. It is based on the idea of Jest Snapshot Testing.
snapshooter alternatives and similar packages
Based on the "Testing" category.
Alternatively, view snapshooter alternatives based on common mentions on social networks and blogs.
-
Bogus
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js. -
Fluent Assertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, as well as .NET Core 2.1, .NET Core 3.0, .NET 6, .NET Standard 2.0 and 2.1. Supports the unit test frameworks MSTest2, NUnit3, XUnit2, MSpec, and NSpec3. -
Testcontainers
A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions. -
AutoFixture
AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data. -
SpecFlow
#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration -
Verify
Verify is a snapshot tool that simplifies the assertion of complex data models and documents. -
NBomber
Modern and flexible load testing framework for Pull and Push scenarios, designed to test any system regardless a protocol (HTTP/WebSockets/AMQP etc) or a semantic model (Pull/Push). -
WireMock.Net
WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality. -
Compare-Net-Objects
What you have been waiting for :+1: Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects. -
Machine.Specifications
Machine.Specifications is a Context/Specification framework for .NET that removes language noise and simplifies tests. -
GenFu
GenFu is a library you can use to generate realistic test data. It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data. You can override any of the fillers, give GenFu hints on how to fill them. -
ArchUnitNET
A C# architecture test library to specify and assert architecture rules in C# for automated testing. -
Expecto
A smooth testing lib for F#. APIs made for humans! Strong testing methodologies for everyone! -
Fine Code Coverage
Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too) -
NFluent
Smooth your .NET TDD experience with NFluent! NFluent is an ergonomic assertion library which aims to fluent your .NET TDD experience (based on simple Check.That() assertion statements). NFluent aims your tests to be fluent to write (with a super-duper-happy 'dot' auto-completion experience), fluent to read (i.e. as close as possible to plain English expression), but also fluent to troubleshoot, in a less-error-prone way comparing to the classical .NET test frameworks. NFluent is also directly inspired by the awesome Java FEST Fluent assertion/reflection library (http://fest.easytesting.org/) -
xBehave.net
DISCONTINUED. ✖ An xUnit.net extension for describing each step in a test with natural language. -
SpecsFor
SpecsFor is a light-weight Behavior-Driven Development framework that focuses on ease of use for *developers* by minimizing testing friction. -
Xunit.Gherkin.Quick
BDD in .NET Core - using Xunit and Gherkin (compatible with both .NET Core and .NET) -
SimpleStubs
*SimpleStubs* is a simple mocking framework that supports Universal Windows Platform (UWP), .NET Core and .NET framework. SimpleStubs is currently developed and maintained by Microsoft BigPark Studios in Vancouver. -
Moq.Contrib.HttpClient
A set of extension methods for mocking HttpClient and IHttpClientFactory with Moq. -
#<Sawyer::Resource:0x00007f89811b9240>
:fire: A small library to help .NET developers leverage Microsoft's dependency injection framework in their Xunit-powered test projects -
SecTester
SecTester is a new tool that integrates our enterprise-grade scan engine directly into your unit tests.
InfluxDB - Purpose built for real-time analytics at any scale.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of snapshooter or a related project?
README
Snapshooter is a snapshot testing tool for .NET Core and .NET Framework
Snapshooter is a flexible snapshot testing tool to simplify the result validation in your unit tests in .Net. It is based on the idea of Jest Snapshot Testing.
To get more detailed information about Snapshooter, go to the Snapshooter Docs
Getting Started
To get started, install the Snapshooter Xunit or NUnit nuget package:
XUnit
dotnet add package Snapshooter.Xunit
NUnit
dotnet add package Snapshooter.NUnit
Assert with Snapshots
To assert your test results with snapshots in your unit tests, follow the following steps:
1. Add snapshot assert statement
Insert a snapshot assert statement Snapshot.Match(yourResultObject);
into your unit test.
Example:
/// <summary>
/// Tests if the new created person is valid.
/// </summary>
[Fact]
public void CreatePersonSnapshotTest()
{
// arrange
var serviceClient = new ServiceClient();
// act
TestPerson person = serviceClient.CreatePerson(
Guid.Parse("2292F21C-8501-4771-A070-C79C7C7EF451"), "David", "Mustermann");
// assert
Snapshot.Match(person);
}
2. Run the unit test to create a new Snapshot
The Snapshot.Match(person)
statement creates a new snapshot of your result object and stores it in the
__snapshots__
folder. The __snapshots__
folder is always next to your executed unit test file.
Snapshot name: <UnitTestClassName>.<TestMethodName>.snap
3. Review new snapshot
Review your new snapshot file __snapshots__/<UnitTestClassName>.<TestMethodName>.snap
.
4. Run unit test to assert
Now the Snapshot.Match(person)
statement will create again a snapshot of your test result and compare it against your reviewed snapshot in the __snapshots__
folder. The __snapshots__
folder is always next to your executed unit test file.
Mismatching Snapshot Handling
If your result object has changed and the existing snapshot is not matching anymore, then the unit test will fail. The unit test error message will point to the exact mismatching position within the snapshot.
In addition, in the snapshot folder __snapshots__
a subfolder with name __mismatch__
will be created. In this folder you can find
the actual snapshot which is mismatching with the existing snapshot in the __snapshots__
folder. Therefore it is possible to compare the two snapshots with a text compare tool.
If the snapshot in the mismatching folder __mismatch__
is correct, just move it to the parent __snapshots__
folder (override the existing one).
Different Match-Syntax Possibilities
The default match syntax for snapshots is:
Snapshot.Match(person);
However, we also could use the fluent syntax:
person.MatchSnapshot();
Or we can use FluentAssertion's should() syntax:
person.Should().MatchSnapshot();
For NUnit we will support the Assert.That syntax (Coming soon):
Assert.That(person, Match.Snapshot());
Features
Ignore Fields in Snapshots Matches
If some fields in your snapshot shall be ignored during snapshot assertion, then the following ignore options can be used:
[Fact]
public void CreatePersonSnapshot_IgnoreId()
{
// arrange
var serviceClient = new ServiceClient();
// act
TestPerson person = serviceClient.CreatePerson("Hans", "Muster");
// assert
Snapshot.Match<Person>(testPerson, matchOptions => matchOptions.IgnoreField("Size"));
}
The fields to ignore will be located via JsonPath, therefore you are very flexible and you can also ignore fields from child objects or arrays.
Ignore Examples:
// Ignores the field 'StreetNumber' of the child node 'Address' of the person
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreField("Address.StreetNumber"));
// Ignores the field 'Name' of the child node 'Country' of the child node 'Address' of the person
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreField("Address.Country.Name"));
// Ignores the field 'Id' of the first person in the 'Relatives' array of the person
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreField("Relatives[0].Id"));
// Ignores the field 'Name' of all 'Children' nodes of the person
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreField("Children[*].Name"));
// Ignores all fields with name 'Id'
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreField("**.Id"));
Ignore All Fields by name
If we want to ignore all fields by a specific name, then we have two options:
Option 1: Use the ignore match option 'IgnoreAllFields()' and add the name.
// Ignores all fields with name 'Id'
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreAllFields("Id"));
Option 2: Use the default ignore match option 'IgnoreFields(*.)' with the following JsonPath syntax *.
// Ignores all fields with name 'Id'
Snapshot.Match<Person>(person, matchOptions => matchOptions.IgnoreFields("**.Id"));
Assert Fields in Snapshots Matches
Sometimes there are fields in a snapshot, which you want to assert separately against another value.
For Example, the Id field of a 'Person' is always newly generated in a service,
therefore you receive in the test always a Person with a new id (Guid).
Now if you want to check that the id is not an empty Guid, the Assert
option can be used.
/// <summary>
/// Tests if the new created person is valid and the person id is not empty.
/// </summary>
[Fact]
public void CreatePersonSnapshot_AssertId()
{
// arrange
var serviceClient = new ServiceClient();
// act
TestPerson person = serviceClient.CreatePerson("Hans", "Muster"); // --> id is created within the service
// assert
Snapshot.Match<Person>(testPerson, matchOption => matchOption.Assert(
fieldOption => Assert.NotEqual(Guid.Empty, fieldOption.Field<Guid>("Id"))));
}
The fields to assert will be located via JsonPath, therefore you are very flexible and you can also ignore fields from child objects or arrays.
Assert Examples:
// Assert the field 'Street' of the 'Address' of the person
Snapshot.Match<Person>(person, matchOption => matchOption.Assert(
fieldOption => Assert.Equal(15, fieldOption.Field<int>("Address.StreetNumber"))));
// Asserts the field 'Code' of the field 'Country' of the 'Address' of the person
Snapshot.Match<Person>(person, matchOption => matchOption.Assert(
fieldOption => Assert.Equal("De", fieldOption.Field<CountryCode>("Address.Country.Code"))));
// Asserts the fist 'Id' field of the 'Relatives' array of the person
Snapshot.Match<Person>(person, > matchOption.Assert(
fieldOption => Assert.NotNull(fieldOption.Field<string>("Relatives[0].Id"))));
// Asserts every 'Id' field of all the 'Relatives' of the person
Snapshot.Match<Person>(person, > matchOption.Assert(
fieldOption => Assert.NotNull(fieldOption.Fields<string>("Relatives[*].Id"))));
// Asserts 'Relatives' array is not empty
Snapshot.Match<Person>(person, > matchOption.Assert(
fieldOption => Assert.NotNull(fieldOption.Fields<TestPerson>("Relatives[*]"))));
The Snapshooter assert functionality is not limited to Xunit or NUnit asserts, it also could be used Fluent Assertions or another assert tool.
Concatenate Ignore & Asserts checks
All the ignore, isType or assert field checks can be concatenated.
[Fact]
public void Match_ConcatenateFieldChecksTest_SuccessfulMatch()
{
// arrange
var serviceClient = new ServiceClient();
// act
TestPerson person = serviceClient.CreatePerson("Hans", "Muster");
// act & assert
Snapshot.Match<TestPerson>(testPerson, matchOption => matchOption
.Assert(option => Assert.NotEqual(Guid.Empty, option.Field<Guid>("Id")))
.IgnoreField<DateTime>("CreationDate")
.Assert(option => Assert.Equal(-58, option.Field<int>("Address.StreetNumber")))
.Assert(option => testChild.Should().BeEquivalentTo(option.Field<TestChild>("Children[3]")))
.IgnoreField<TestCountry>("Address.Country")
.Assert(option => Assert.Null(option.Field<TestCountry>("Relatives[0].Address.Plz"))));
}
Using Snapshooter in CI-Builds
When running snapshooter tests in a CI-build you might want to ensure that a snapshots are correctly checked-in since otherwise tests without a snapshot will just create the initial snapshot and become green.
In order to fail tests that are without a snapshot on your CI-build you can set the snapshooter behavior to strict-mode by setting the environment variable SNAPSHOOTER_STRICT_MODE
to on
or true
.
Community
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the Swiss Life OSS Code of Conduct.