pietgk / Snapper

Bringing Jest-esque Snapshot testing to C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snapper

Bringing Jest-esque Snapshot testing to C#

AppVeyor GitHub release NuGet Documentation WIP

Snapper is a NuGet library which captures snapshots of objects to simplify testing. It is very heavily based on Jest Snapshot Testing.

Getting Started

Currently Snapper consists of three different NuGet packages for extensibility.

Choose the package which best fits your needs

  • Snapper.Core: Basic snapshot functionality. Stores snapshots in bytes. Use for extending Snapper.
  • Snapper.Json: Extends Snapper.Core to provide storing snapshots in Json format
  • Snapper.Json.Xunit: Extends Snapper.Json. Adds extensions to XUnit Assert.

Install the package through NuGet

nuget install <package_name>

Using Snapper

Snapper.Core

// Create class which implements IAssert
var asserter = new ClassWhichImplementsIAssert();

var snapper = new Snapper(asserter, directoryToStoreSnapshots);

// the object to snapshot must be marked as `Serializable`
snapper.Snap("snapshotName", objectToSnapshot);

To update snapshots set the Environment Variable UpdateSnapshots to true and run the tests.

Snapper.Json

// Create class which implements IAssert
var asserter = new ClassWhichImplementsIAssert();

var snapper = new JsonSnapper(asserter, directoryToStoreSnapshots);

snapper.Snap("snapshotName", objectToSnapshot);

To update snapshots set the Environment Variable UpdateSnapshots to true and run the tests.

Snapper.Json.Xunit

This package extends XUnits Assert class and therefore conflicts with xunit.assert package. You can safely remove the xunit.assert package and just use what is imported through this package.

// Snapshot name will be the same as the name of the test
Assert.MatchSnapshot(objectToSnapshot);

Assert.MatchSnapshot(snapshotName, objectToSnapshot);

To update snapshots set the Environment Variable UpdateSnapshots to true and run the tests. You can also add the [UpdateTestSnapshot] attribute to your test and run it. (Remember to remove it before you commit your code)

Todo

  • Write tests
  • Extend XUnit Assert e.g. Assert.Snap(obj) rather than XUnitSnapper.Snap(obj)
  • Write wiki docs
  • Write method/class docs
  • Update appveyor to build on every commit and publish nuget on tag
  • Create sample project
  • Publish to Nuget
  • Add tags to Nuget
  • Add logo to Nuget
  • Downgrade project to lowest .net standard possible
  • Downgrade nuget dependencies to lowest possible

About

Bringing Jest-esque Snapshot testing to C#

License:MIT License


Languages

Language:C# 100.0%