Documentation ¶
Overview ¶
Package snap supports source-based snapshot testing. Package snap maintains expected or "golden" values in _test.go files and can update the expected value by rewriting the source.
To make a snapshot test, create a snapshot with Source and test the snapshot with a Check function such as CheckString. Then, call Run in a TestMain function and run tests with "go test -update-snapshots" to update snapshots.
As an example, this is a complete test file with Source, CheckString, and Run. The snapshot is out of date: It should mention "complicated value" but instead it is "old".
package example_test import ( "testing" "github.com/jellevandenhooff/snap" ) func TestSnapshot(t *testing.T) { magic := "complicated value" snap.CheckString(t, magic, snap.Source("old")) } func TestMain(m *testing.M) { snap.Run(m) }
Running "go test" will fail because the snapshot is out of date. Running "go test -update-snapshots" will update the snapshot in the file to "snap.Source(`complicated value`)" and afterwards tests will pass.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckString ¶
CheckString compares a string with a snapshot.
Types ¶
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
A Snapshot is an expected value in a test created by calling Source. See Source on how to use Snapshots.
func Source ¶
Source creates a automatically-updated Snapshot.
A Snapshot must be passed to a Check function like CheckJSON to be tested. Each Snapshot can be used only once.
The argument to Source can be automatically updated against actual values using the "go test -update-snapshots" flag; see Run.
Only one call to Source per line is supported.