autogold

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2020 License: Apache-2.0, MIT, Apache-2.0, + 1 more Imports: 12 Imported by: 2

README

autogold - automated Go golden file testing Hexops logo

Go Reference

Instead of writing your desired test output as a large Go structure / string in your code, simply write:

autogold.Equal(t, got)

The test output (nested Go struct, string, etc.) will be formatted using google/go-cmp. If the testdata/<test name>.golden snapshot file is different, the test will fail with a nice multi-line diff and go test -update will update the file if you like the changes.

Example usage

Import the package:

import "github.com/hexops/autogold"

Write a test that produces any Go value (got):

func TestFoo(t *testing.T) {
    got := Bar()
    autogold.Equal(t, got)
}

Run go test and you'll see the test fails:

--- FAIL: TestFoo (0.00s)
    autogold.go:148: mismatch (-want +got):
        --- want
        +++ got
        @@ -1 +1,2 @@
        +{*example.Baz}.Name:"Jane"
        +{*example.Baz}.Age:31

We see a diff showing what our test produced and what we expected (nothing, because testdata/TestFoobar.golden does not exist.)

Rerun the test with go test -update and testdata/TestFoobar.golden will be created/updated with the output we got.

When should golden files be used?

Golden files are used by the Go authors for testing the standard library, the gofmt tool, etc. and are a common pattern in the Go community for snapshot testing. See also "Testing with golden files in Go" - Chris Reeves

Golden files make the most sense when you'd otherwise have to write a complex multi-line string or large Go structure inline in your test.

Custom formatting

google/go-cmp is used to produce a text description of the Go value you provide to autogold.Equal. If the default output is not suitable for your test, you have options:

Changing formatting for a specific sub-value

If your type implements the fmt.GoStringer interface, it will be used to convert your type to a string.

Include unexported fields
autogold.Equal(t, got, autogold.Unexported())
Use your own custom format (JSON, etc.)

Simply pass a string value to autogold.Equal, doing the formatting yourself. It'll be written to the golden file as-is.

Provide custom go-cmp options

You can provide any go-cmp option which will affect formatting by providing the autogold.CmpOptions(...) option to autogold.Equal.

Alternatives

Before writing autogold, I considered the following alternatives but was left wanting a better API:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t *testing.T, got interface{}, opts ...Option)

Equal checks if got is equal to the saved testdata/.golden test file. If it is not, t.Fatal is called with a multi-line diff comparison.

If the go test -update flag is specified, the .golden files will be updated or created automatically.

Custom equality operators can be used if needed by passing options. See https://pkg.go.dev/github.com/google/go-cmp/cmp

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures specific behavior for Equal.

func CmpOptions

func CmpOptions(opts ...cmp.Option) Option

CmpOptions specifies go-cmp options that should be used.

func Dir

func Dir(dir string) Option

Dir specifies a customer directory to use for writing the golden files, instead of the default "testdata/".

func Name

func Name(name string) Option

Name specifies a name to use for the testdata/<name>.golden file instead of the default test name.

func Unexported

func Unexported() Option

Unexported is an option that includes unexported fields in the output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL