# README
Incredibly simple Go snapshot testing: cupaloy
takes a snapshot of your test output and compares it to a snapshot committed alongside your tests. If the values don't match then you'll be forced to update the snapshot file before the test passes.
Snapshot files are handled automagically: just use the cupaloy.SnapshotT(t, value)
function in your tests and cupaloy
will automatically find the relevant snapshot file and compare it with the given value.
Usage
func TestExample(t *testing.T) {
result := someFunction()
// check that the result is the same as the last time the snapshot was updated
// if the result has changed then the test will be failed with an error containing
// a diff of the changes
cupaloy.SnapshotT(t, result)
}
To update the snapshots simply set the UPDATE_SNAPSHOTS
environment variable and run your tests e.g.
UPDATE_SNAPSHOTS=true go test ./...
This will fail all tests where the snapshot was updated (to stop you accidentally updating snapshots in CI) but your snapshot files will now have been updated to reflect the current output of your code.
Installation
go get -u github.com/bradleyjkemp/cupaloy
Further Examples
Table driven tests
var testCases = map[string][]string{
"TestCaseOne": []string{......},
"AnotherTestCase": []string{......},
....
}
func TestCases(t *testing.T) {
for testName, args := range testCases {
t.Run(testName, func(t *testing.T) {
result := functionUnderTest(args...)
cupaloy.SnapshotT(t, result)
})
}
}
Changing output directory
func TestSubdirectory(t *testing.T) {
result := someFunction()
snapshotter := cupaloy.New(cupaloy.SnapshotSubdirectory("testdata"))
err := snapshotter.Snapshot(result)
if err != nil {
t.Fatalf("error: %s", err)
}
}
For further usage examples see basic_test.go and advanced_test.go in the examples/ directory which are both kept up to date and run on CI.