-1.6 C
New York
Saturday, December 21, 2024

Testing snapshots in .NET with Storm Petrel


The time period “Snapshot Testing” originates from capturing an precise display screen snapshot and evaluating it to an anticipated snapshot. Nevertheless, the time period just isn’t restricted to photographs and extends to different sorts of precise code execution outcomes, resembling HTML, JSON, XML, PDF, and many others. Sometimes, snapshot unit/integration testing entails the next steps in a check technique:

  • Organize: Create the anticipated snapshot.
  • Act: Seize the precise snapshot.
  • Say: Examine the anticipated and precise snapshots to find out equality.

As we all know, the Scand storm petrel NuGet Packages automate the technology/rewriting of anticipated baselines with actual values. Let’s discover what they’ll do within the context of snapshot values.

Testing snapshots with Scand.StormPetrel.Generator

The Scand.StormPetrel.Generator NuGet package deal implements automated technology/rewriting of any sort of anticipated values ​​saved in C# code. Thus, we will conclude:

  • HTML, JSON, XML, and different textual content snapshots could be traced as C# code values, as seen within the check strategies of the SnapshotTest class as documented. use circumstances.
  • Binary snapshots can be traced as C# code values, as demonstrated in the identical SnapshotTest instance.

Due to this fact, the moment unit/integration check steps change into common unit/integration check steps, even with out together with Scand.StormPetrel.Generator:

  • Organize: Create the anticipated snapshot worth as a variable, a check attribute parameter, or a worth within the physique of a check knowledge supply technique.
  • Act: Seize the precise snapshot by working the C# code being examined.
  • Say: Examine anticipated and precise snapshots for equality utilizing no matter assertion library/technique the developer prefers.

Testing snapshots 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 primary use case of Scand.StormPetrel.Generator:

Within the context of snapshot testing, a developer can use advantages resembling:

  • Simpler detection and assessment of anticipated values ​​of particular properties tracked in exams utilizing the “Discover All References” command in Visible Studio or comparable instructions in different IDEs.
  • Simpler renaming of particular properties in each regular code and check code.
  • Simpler assertion of precise/anticipated values ​​with assertion library strategies like BeEquivalentTo by FluentAssertionsfor instance, when the order of properties or array components within the anticipated baseline doesn’t matter, or some properties could also be ignored.
  • Sooner check execution in typical circumstances.

Testing snapshots with Scand.StormPetrel.FileSnapshotInfrastructure

In some circumstances, it is sensible to maintain snapshots as particular person recordsdata on the file system. A developer can use advantages like:

  • Simpler assessment and comparability of anticipated snapshots by way of specialised software program for photographs, JSON, XML, PDF or different viewers/comparators.
  • Simpler integration of .NET exams with different applied sciences. An actual instance is when the anticipated JSON snapshots for a .NET RESTful API service change into enter knowledge for JavaScript/TypeScript unit exams of a shopper software for the service.
  • Sooner check execution in some circumstances, resembling when a efficiency check must validate massive recordsdata.

The anticipated snapshot recordsdata can be generated/rewritten with Scand StormPetrel. It is best to use Scand.StormPetrel.FileSnapshotInfrastructure on this case in keeping with your documentation. The moment unit/integration testing steps are similar to the common unit/integration testing steps and change into:

  • Organize: Learn the anticipated snapshot worth of a file. You should name Scand.StormPetrel.FileSnapshotInfrastructure.SnapshotProvider.ReadAllText technique or use different options described within the use circumstances.
  • Act: Seize the precise snapshot by working the C# code being examined.
  • Say: Examine anticipated and precise snapshots for equality utilizing no matter assertion library/technique the developer prefers.

Conclusions

Scandinavian storm petrel makes use of the NuGet and .NET incremental generator infrastructure and could be actively utilized in snapshot testing. With minimal or no negative effects on the construction and conduct of unit/integration snapshot exams, it effectively allows anticipated snapshot technology/rewriting, accelerating and simplifying software program growth. For extra info, please contact us.

Related Articles

Latest Articles