百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

snapshooter

> 测试质量
开源

Snapshooter 是.NET Core 和.NET框架的快照测试工具

331 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Snapshooter 是.NET Core 和.NET框架的快照测试工具

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

bash
dotnet add package Snapshooter.Xunit

NUnit

bash
dotnet add package Snapshooter.NUnit

MSTest

bash
dotnet add package Snapshooter.MSTest

Get Started

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:

csharp
/// 
/// Tests if the new created person is valid.
/// 
[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: ..snap

3. Review new snapshot

Review your new snapshot file __snapshots__/..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).

Read More

Different Match-Syntax Possibilities

The default match syntax for snapshots is:

csharp
    Snapshot.Match(person);

However, we also could use the fluent syntax:

csharp
    person.MatchSnapshot();

Or we can use FluentAssertion's should() syntax:

csharp
    person.Should().MatchSnapshot();

For NUnit we will support the Assert.That syntax (Coming soon):

csharp
    Assert.That(person, Match.Snapshot());

Features

Ignore Fields in Snapshots

If some fields in your snapshot shall be ignored during snapshot assertion, then the following ignore options can be used:

csharp
[Fact]
public void CreatePersonSnapshot_IgnoreId()
{
    // arrange
    var serviceClient = new ServiceClient();

    // act
    TestPerson person = serviceClient.CreatePerson("Hans", "Muster");

    // assert
    Snapshot.Match(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:

…

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.

csharp
// Ignores all fields with name 'Id'
Snapshot.Match(person, matchOptions => matchOptions.IgnoreAllFields("Id"));

Option 2: Use the default ignore match option 'IgnoreFields(**.)' with the following JsonPath syntax **.

csharp
// Ignores all fields with name 'Id'
Snapshot.Match(person, matchOptions => matchOptions.IgnoreFields("**.Id"));

Hash Fields in Snapshots

If some fields of our snapshot are too big, for example a binary field with a lot of data, then we can use the HashField option. The HashField option creates a Hash of the field value and therefore each time only the hash is compared. If there is a change in the field value, then the snapshot match will fail.

csharp
[Fact]
public void ImageSnapshot_HashImageBinary()
{
    // arrange
    var serviceClient = new ServiceClient();

    // act
    TestImage image = serviceClient.CreateMonaLisaImage();

    // assert
    Snapshot.Match(image, matchOptions => matchOptions.HashField("Data"));
}

Example Snapshot with Hash

json
{
  "Id": 3450987,
  "OwnerId": "0680faef-6e89-4d52-bad8-291053c66696",
  "Name": "Mona Lisa",
  "CreationDate": "2020-11-10T21:23:09.036+01:00",
  "Price": 951868484.345,
  "Data": "m+sQR9KG9WpgYoQiRASPkt9FLJOLsjK86UuiXKVRzas="  
}

The field(s) to hash can be located via JsonPath or via field name.

Hash Field Examples:

…

Assert Fields in Snapshots

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.

csharp
/// 
/// Tests if the new created person is valid and the person id is not empty.
/// 
[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(testPerson, matchOption => matchOption.Assert(
                    fieldOption => Assert.NotEqual(Guid.Empty, fieldOption.Field("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:

…

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.

…

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#csharpjsonnetsnapshot

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.