Skip to content

Snapshot Testing in .NET with Scand Storm Petrel

The term “Snapshot Testing” originates from capturing an actual screen snapshot and comparing it with an expected snapshot. However, the term is not limited to images and extends to other types of actual code execution results, such as HTML, JSON, XML, PDF, etc. Typically, Snapshot Unit/Integration Testing involves the following steps in a test method:

  • Arrange: Create the expected snapshot.
  • Act: Capture the actual snapshot.
  • Assert: Compare the expected and actual snapshots for equality.

As we know, the Scand Storm Petrel NuGet packages automate the generation/rewriting of expected baselines with actual values. Let’s explore what they can do in the context of snapshot values.

Snapshot Testing with Scand.StormPetrel.Generator

The Scand.StormPetrel.Generator NuGet package implements automated generation/rewriting of any type of expected values kept in C# code. Thus, we can conclude:

  • HTML, JSON, XML, and other text snapshots can be tracked as C# code values, as seen in the test methods of the SnapshotTest class according to the documented use cases.
  • Binary snapshots can also be tracked as C# code values, as demonstrated in the same SnapshotTest example.

Therefore, Snapshot Unit/Integration Testing steps become regular steps of unit/integration testing, even without including Scand.StormPetrel.Generator:

  • Arrange: Create the expected snapshot value as a variable, test attribute parameter, or a value in a test data source method body.
  • Act: Capture the actual snapshot by executing the C# code being tested.
  • Assert: Compare the expected and actual snapshots for equality using any assertion library/methods the developer prefers.

Snapshot Testing with Scand.StormPetrel.Generator without Serialization

In many cases, .NET developers can avoid serializing actual values to JSON, XML, or other formats and instantiate the expected snapshot directly in C# code, which corresponds to the primary use case of Scand.StormPetrel.Generator:

StormPetrel.Generator

In the context of Snapshot Testing, a developer can utilize benefits such as:

  • Easier detection and review of specific property expected values tracked in the tests via the “Find All References” command in Visual Studio or similar commands in other IDEs.
  • Easier renaming of specific properties in both regular code and test code.
  • Easier assertion of actual/expected values with assertion library methods like FluentAssertions’ BeEquivalentTo, e.g., when the order of properties or array elements in the expected baseline does not matter, or some properties can be ignored.
  • Faster test execution in typical cases.

Snapshot Testing with Scand.StormPetrel.FileSnapshotInfrastructure

In some cases, it makes sense to keep snapshots as individual files in the file system. A developer can utilize benefits such as:

  • Easier review and comparison of expected snapshots via specialized software for images, JSON, XML, PDF, or other viewers/comparers.
  • Easier integration of .NET tests with other technologies. A real example is when expected JSON snapshots for a .NET RESTful API Service become input data for JavaScript/TypeScript unit tests of a client application for the service.
  • Faster test execution in some cases, such as when a performance test needs to assert large files.

The expected snapshot files can also be generated/rewritten with Scand StormPetrel. You need to utilize the Scand.StormPetrel.FileSnapshotInfrastructure in this case according to its documentation. Snapshot Unit/Integration Testing steps are very similar to regular steps of unit/integration testing and become:

  • Arrange: Read the expected snapshot value from a file. You need to call the Scand.StormPetrel.FileSnapshotInfrastructure.SnapshotProvider.ReadAllText method or use other alternatives described in the use cases.
  • Act: Capture the actual snapshot by executing the C# code being tested.
  • Assert: Compare the expected and actual snapshots for equality using any assertion library/methods the developer prefers.

Conclusions

Scand Storm Petrel utilizes NuGet and .NET Incremental Generators infrastructure and can be actively used in Snapshot Testing. With minimal or no side effects on unit/integration snapshot test structure and behavior, it efficiently allows the generation/rewriting of expected snapshots, speeding up and simplifying software development. For more information, please contact us.

Author Bio
Alexander Bąk Head of Web Development Department
Alexander has 20 years of experience in software development, delivering new and innovative solutions for a myriad of global companies, ranging from small startups to large-scale enterprises. His main focus areas are web development and front-end development.
Need Mobile Developers?

At SCAND you can hire mobile app developers with exceptional experience in native, hybrid, and cross-platform app development.

Mobile Developers Mobile Developers
Looking for Java Developers?

SCAND has a team of 50+ Java software engineers to choose from.

Java Developers Java Developers
Looking for Skilled .NET Developers?

At SCAND, we have a pool of .NET software developers to choose from.

NET developers NET developers
Need to Hire Professional Web Developers Fast and Easy?

Need to Hire Professional Web Developers Fast and Easy?

Web Developers Web Developers
Need to Staff Your Team With React Developers?

Our team of 25+ React engineers is here at your disposal.

React Developers React Developers
Searching for Remote Front-end Developers?

SCAND is here for you to offer a pool of 70+ front end engineers to choose from.

Front-end Developers Front-end Developers
Other Posts in This Category
View All Posts

This site uses technical cookies and allows the sending of 'third-party' cookies. By continuing to browse, you accept the use of cookies. For more information, see our Privacy Policy.