Thursday, November 21, 2024

Snapshot Testing in .NET with Storm Petrel

Software DevelopmentSnapshot Testing in .NET with 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:

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.

Check out our other content

Check out other tags:

Most Popular Articles