The time period “Snapshot Testing” originates from capturing an precise display screen snapshot and evaluating it with an anticipated snapshot. Nevertheless, the time period is just not restricted to photographs and extends to different varieties of precise code execution outcomes, comparable to HTML, JSON, XML, PDF, and so on. Usually, Snapshot Unit/Integration Testing entails the next steps in a take a look at technique:
- Prepare: Create the anticipated snapshot.
- Act: Seize the precise snapshot.
- Assert: Examine 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 will 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 saved in C# code. Thus, we are able to conclude:
- HTML, JSON, XML, and different textual content snapshots could be tracked as C# code values, as seen within the take a look at strategies of the SnapshotTest class in accordance with the documented use instances.
- Binary snapshots may also be tracked as C# code values, as demonstrated in the identical SnapshotTest instance.
Subsequently, Snapshot Unit/Integration Testing steps grow 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 worth in a take a look at information supply technique physique.
- Act: Seize the precise snapshot by executing the C# code being examined.
- Assert: Examine 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 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 comparable to:
- Simpler detection and overview of particular property anticipated values tracked within the checks by way of the “Discover All References” command in Visible Studio or related 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 components within the anticipated baseline doesn’t matter, or some properties could be ignored.
- Quicker take a look at execution in typical instances.
Snapshot Testing with Scand.StormPetrel.FileSnapshotInfrastructure
In some instances, it is sensible to maintain snapshots as particular person information within the file system. A developer can make the most of advantages comparable to:
- Simpler overview and comparability of anticipated snapshots by way of specialised software program for photos, 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 grow to be enter information for JavaScript/TypeScript unit checks of a consumer utility for the service.
- Quicker take a look at execution in some instances, comparable to when a efficiency take a look at wants to claim giant information.
The anticipated snapshot information may also be generated/rewritten with Scand StormPetrel. You might want to 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 grow to be:
- Prepare: Learn the anticipated snapshot worth from a file. You might want to 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: Examine 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 could be actively utilized in Snapshot Testing. With minimal or no unwanted effects on unit/integration snapshot take a look at construction and conduct, it effectively permits the technology/rewriting of anticipated snapshots, rushing up and simplifying software program improvement. For extra data, please contact us.