-15.8 C
United States of America
Tuesday, January 21, 2025

Snapshot Testing in .NET with Storm Petrel


The time period “Snapshot Testing” originates from capturing an precise display snapshot and evaluating it with an anticipated snapshot. Nonetheless, the time period isn’t restricted to pictures and extends to different forms of precise code execution outcomes, equivalent to HTML, JSON, XML, PDF, and so on. Sometimes, Snapshot Unit/Integration Testing entails the next steps in a check technique:

  • Organize: 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 era/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 era/rewriting of any kind of anticipated values saved in C# code. Thus, we will conclude:

  • HTML, JSON, XML, and different textual content snapshots might be tracked as C# code values, as seen within the check strategies of the SnapshotTest class in accordance with the documented use instances.
  • Binary snapshots can 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:

  • Organize: Create the anticipated snapshot worth as a variable, check attribute parameter, or a worth in a check knowledge supply technique 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 instances, .NET builders can keep away from serializing precise values to JSON, XML, or different codecs and instantiate the anticipated snapshot immediately 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 exams 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 check code.
  • Simpler assertion of precise/anticipated values with assertion library strategies like FluentAssertions’ BeEquivalentTo, e.g., when the order of properties or array components within the anticipated baseline doesn’t matter, or some properties might be ignored.
  • Sooner check execution in typical instances.

Snapshot Testing with Scand.StormPetrel.FileSnapshotInfrastructure

In some instances, it is smart to maintain snapshots as particular person information 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 exams 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 exams of a shopper utility for the service.
  • Sooner check execution in some instances, equivalent to when a efficiency check wants to claim giant information.

The anticipated snapshot information can be generated/rewritten with Scand StormPetrel. You could make the most of the Scand.StormPetrel.FileSnapshotInfrastructure on this case in accordance with its documentation. Snapshot Unit/Integration Testing steps are similar to common steps of unit/integration testing and turn out to be:

  • Organize: Learn the anticipated snapshot worth from a file. You could name the Scand.StormPetrel.FileSnapshotInfrastructure.SnapshotProvider.ReadAllText technique or use different alternate options described within the use instances.
  • 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 Turbines infrastructure and might be actively utilized in Snapshot Testing. With minimal or no unintended effects on unit/integration snapshot check construction and habits, it effectively permits the era/rewriting of anticipated snapshots, rushing up and simplifying software program improvement. For extra info, please contact us.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles