The time period “Snapshot Testing” originates from capturing an precise display screen snapshot and evaluating it with an anticipated snapshot. Nonetheless, the time period just isn’t restricted to photographs and extends to different sorts of precise code execution outcomes, corresponding to HTML, JSON, XML, PDF, and so forth. Usually, Snapshot Unit/Integration Testing includes the next steps in a check 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 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 bundle implements automated era/rewriting of any sort 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 line with the documented use cases.
- Binary snapshots may also be tracked as C# code values, as demonstrated in the identical SnapshotTest instance.
Due to this fact, Snapshot Unit/Integration Testing steps turn into common steps of unit/integration testing, even with out together with Scand.StormPetrel.Generator:
- Prepare: Create the anticipated snapshot worth as a variable, check attribute parameter, or a worth in a check 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 straight in C# code, which corresponds to the first use case of Scand.StormPetrel.Generator:
Within the context of Snapshot Testing, a developer can make the most of advantages corresponding to:
- Simpler detection and evaluate of particular property anticipated values tracked within the assessments through the “Discover All References” command in Visible Studio or related 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 circumstances.
Snapshot Testing with Scand.StormPetrel.FileSnapshotInfrastructure
In some circumstances, it is sensible to maintain snapshots as particular person information within the file system. A developer can make the most of advantages corresponding to:
- Simpler evaluate and comparability of anticipated snapshots through specialised software program for photographs, JSON, XML, PDF, or different viewers/comparers.
- Simpler integration of .NET assessments with different applied sciences. An actual instance is when anticipated JSON snapshots for a .NET RESTful API Service turn into enter knowledge for JavaScript/TypeScript unit assessments of a consumer software for the service.
- Sooner check execution in some circumstances, corresponding to when a efficiency check wants to say giant information.
The anticipated snapshot information may also be generated/rewritten with Scand StormPetrel. That you must make the most of the Scand.StormPetrel.FileSnapshotInfrastructure on this case in line with its documentation. Snapshot Unit/Integration Testing steps are similar to common steps of unit/integration testing and turn into:
- Prepare: Learn the anticipated snapshot worth from a file. That you must name the Scand.StormPetrel.FileSnapshotInfrastructure.SnapshotProvider.ReadAllText methodology or use different options described within the use cases.
- 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 unwanted effects on unit/integration snapshot check construction and conduct, it effectively permits the era/rewriting of anticipated snapshots, dashing up and simplifying software program growth. For extra data, please contact us.