Categorygithub.com/plouc/gosnap
repositorypackage
0.0.0-20180714070049-61403c2f226e
Repository: https://github.com/plouc/gosnap.git
Documentation: pkg.go.dev

# README

gosnap

Build Status GoDoc GitHub license Go Report Card GitHub issues

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.

Install

go get github.com/plouc/gosnap

Usage

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.