gosnap is a go package to perform snapshot testing.
Snapshot testing is a convenient way to test large outputs with ease,
this package was initially created to test the go-gitlab-client
CLI.
go get github.com/plouc/gosnap
gosnap requires a context to run, from which you can create snapshots.
package whatever
import (
"testing"
"github.com/plouc/gosnap"
)
func TestSomething(t *testing.T) {
// creates a new context
// snapshots will be stored inside the `snapshots` directory
ctx := gosnap.NewContext(t, "snapshots")
// creates a new snapshot
// it will be stored in `snapshots/my_first_snapshot.snap`
s := ctx.NewSnapshot("my_first_snapshot")
actual := DoSomethingWhichReturnsString()
// this will load the snapshot content and check it matches `actual`
s.AssertString(actual)
}
This will check that actual
matches current snapshot (./snapshots/my_first_snapshot.snap
) content.
The first time you run your tests, the snapshot will be created automatically,
then if the current result does not match snapshot's content, you'll have to
update it, you can add a command-line flag to the go test command
to do so:
# will update all stale snapshots
go test -v ./... -args -update all
# will just update snapshot whose name is `my_snapshot`
go test -v ./... -args -update my_snapshot
For complete usage of gosnap, see the full package docs.