-8.3 C
New York
Sunday, December 22, 2024

Snapshot Testing in .NET with Storm Petrel


The time period “Snapshot Testing” originates from capturing an precise display screen snapshot and evaluating it with an anticipated snapshot. Nonetheless, the time period is just not restricted to pictures and extends to different sorts of precise code execution outcomes, equivalent to HTML, JSON, XML, PDF, and so forth. Usually, Snapshot Unit/Integration Testing entails the next steps in a take a look at methodology:

  • Prepare: Create the anticipated snapshot.
  • Act: Seize the precise snapshot.
  • Assert: Evaluate the anticipated and precise snapshots for equality.

As we all know, the Scand Storm Petrel NuGet packages automate the technology/rewriting of anticipated baselines with precise values. Let’s discover what they’ll do within the context of snapshot values.

Snapshot Testing with Scand.StormPetrel.Generator

The Scand.StormPetrel.Generator NuGet package deal implements automated technology/rewriting of any kind of anticipated values stored in C# code. Thus, we are able to conclude:

  • HTML, JSON, XML, and different textual content snapshots might be tracked as C# code values, as seen within the take a look at strategies of the SnapshotTest class in keeping with the documented use circumstances.
  • Binary snapshots may also be tracked as C# code values, as demonstrated in the identical SnapshotTest instance.

Subsequently, Snapshot Unit/Integration Testing steps turn out to be common steps of unit/integration testing, even with out together with Scand.StormPetrel.Generator:

  • Prepare: Create the anticipated snapshot worth as a variable, take a look at attribute parameter, or a price in a take a look at knowledge supply methodology physique.
  • Act: Seize the precise snapshot by executing the C# code being examined.
  • Assert: Evaluate the anticipated and precise snapshots for equality utilizing any assertion library/strategies the developer prefers.

Snapshot Testing with Scand.StormPetrel.Generator with out Serialization

In lots of circumstances, .NET builders can keep away from serializing precise values to JSON, XML, or different codecs and instantiate the anticipated snapshot instantly in C# code, which corresponds to the first use case of Scand.StormPetrel.Generator:

Snapshot Testing in .NET with Storm Petrel

Within the context of Snapshot Testing, a developer can make the most of advantages equivalent to:

  • Simpler detection and evaluation of particular property anticipated values tracked within the checks by way of the “Discover All References” command in Visible Studio or comparable instructions in different IDEs.
  • Simpler renaming of particular properties in each common code and take a look at code.
  • Simpler assertion of precise/anticipated values with assertion library strategies like FluentAssertions’ BeEquivalentTo, e.g., when the order of properties or array parts within the anticipated baseline doesn’t matter, or some properties might be ignored.
  • Quicker take a look at execution in typical circumstances.

Snapshot Testing with Scand.StormPetrel.FileSnapshotInfrastructure

In some circumstances, it is sensible to maintain snapshots as particular person recordsdata within the file system. A developer can make the most of advantages equivalent to:

  • Simpler evaluation and comparability of anticipated snapshots by way of specialised software program for photographs, JSON, XML, PDF, or different viewers/comparers.
  • Simpler integration of .NET checks with different applied sciences. An actual instance is when anticipated JSON snapshots for a .NET RESTful API Service turn out to be enter knowledge for JavaScript/TypeScript unit checks of a consumer software for the service.
  • Quicker take a look at execution in some circumstances, equivalent to when a efficiency take a look at wants to say giant recordsdata.

The anticipated snapshot recordsdata may also be generated/rewritten with Scand StormPetrel. It is advisable make the most of the Scand.StormPetrel.FileSnapshotInfrastructure on this case in keeping with its documentation. Snapshot Unit/Integration Testing steps are similar to common steps of unit/integration testing and turn out to be:

  • Prepare: Learn the anticipated snapshot worth from a file. It is advisable name the Scand.StormPetrel.FileSnapshotInfrastructure.SnapshotProvider.ReadAllText methodology or use different options described within the use circumstances.
  • Act: Seize the precise snapshot by executing the C# code being examined.
  • Assert: Evaluate the anticipated and precise snapshots for equality utilizing any assertion library/strategies the developer prefers.

Conclusions

Scand Storm Petrel makes use of NuGet and .NET Incremental Mills infrastructure and might be actively utilized in Snapshot Testing. With minimal or no unintended effects on unit/integration snapshot take a look at construction and habits, it effectively permits the technology/rewriting of anticipated snapshots, dashing up and simplifying software program improvement. For extra data, please contact us.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles