goldie

package module
v2.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2020 License: MIT Imports: 15 Imported by: 10

README

goldie - Golden test utility for Go

GoDoc Go Go Report Card

goldie is a golden file test utility for Go projects. It's typically used for testing responses with larger data bodies.

The concept is straight forward. Valid response data is stored in a "golden file". The actual response data will be byte compared with the golden file and the test will fail if there is a difference.

Updating the golden file can be done by running go test -update ./....

See the GoDoc for API reference and configuration options.

Installation

Install the latest version, v2, with:

go get -u github.com/sebdah/goldie/v2

For the older v1 release, please use:

go get -u github.com/sebdah/goldie

Example usage

Basic assertions

The below example fetches data from a REST API. The last line in the test is the actual usage of goldie. It takes the HTTP response body and asserts that it's what is present in the golden test file.

func TestExample(t *testing.T) {
    recorder := httptest.NewRecorder()

    req, err := http.NewRequest("GET", "/example", nil)
    assert.Nil(t, err)

    handler := http.HandlerFunc(ExampleHandler)
    handler.ServeHTTP()

    g := goldie.New(t)
    g.Assert(t, "example", recorder.Body.Bytes())
}

Assertions using templates

If some values in the golden file can change depending on the test, you can use golang template in the golden file and pass the data to AssertWithTemplate.

example.golden
This is a {{ .Type }} file.
Test
func TestTemplateExample(t *testing.T) {
    recorder := httptest.NewRecorder()

    req, err := http.NewRequest("POST", "/example/Golden", nil)
    assert.Nil(t, err)

    handler := http.HandlerFunc(ExampleHandler)
    handler.ServeHTTP()

    data := struct {
        Type	string
    }{
        Type:	"Golden",
    }

    g := goldie.New(t)
    g.AssertWithTemplate(t, "example", data, recorder.Body.Bytes())
}

Then run your test with the -update flag the first time to store the result.

go test -update ./...

For any consecutive runs where you actually want to compare the data, simply drop the -update flag.

go test ./...

Validating JSON and XML output

If you are asserting JSON and XML data, you can use the handy AssertJson and AssertXml functions that will nicely indent the golden validation files for better readability.

Flags

Clean output directory

Using -update along with -clean flag will clear the fixture directory before updating golden files.

go test -update -clean ./...

Options

goldie supports a number of configuration options that will alter the behavior of the library. These options should be passed into the goldie.New() method.

func TestNewExample(t *testing.T) {
    g := goldie.New(
        t,
        goldie.WithFixtureDir("test-fixtures"),
        goldie.WithNameSuffix(".golden.json"),
        goldie.WithDiffEngine(goldie.ColoredDiff),
        goldie.WithTestNameForDir(true),
    )

    g.Assert(t, "example", []byte("my example data"))
}

Available options

Option Comment Default
WithFixtureDir Set fixture dir name testdata
WithNameSuffix Suffix for fixture files. .golden
WithDirPerms Directory permissions for fixtures 0755
WithFilePerms File permissions for fixtures 0644
WithDiffEngine Diff engine to use for diff output ClassicDiff
WithDiffFn Custom diff logic to be used None
WithIgnoreTemplateErrors Ignore errors from templates false
WithTestNameForDir Create a folder with the tests name for the fixtures false
WithSubTestNameForDir Create a folder with the sub tests name for the fixtures false

Diff output

Goldie has three output modes; classic diff (default), colored diffs and simple mode.

You can select your preferred output using the WithDiffEngine option:

g.New(
    t,
    goldie.WithDiffEngine(goldie.ColoredDiff), // Simple, ColoredDiff, ClassicDiff
)

Goldie v2

With the release of Goldie v2.0.0 we are introducing features that will break backwards compatibility with older versions of the test helper. A few things have changed:

New default fixture directory

There is a new default directory for fixtures, testdata. This directory is a better default as it is more widely used in the Go community (including the standard library). See issue #10 for details.

New way to initialize Goldie

With the introduction of the functional options we also introduced goldie.New, which is initializing Goldie. Assert* and other methods are now accessed like:

g := goldie.New(t)
g.Assert(t, ...)

FAQ

Do you need any help in the project?

Yes, please! Pull requests are most welcome. On the wish list:

  • Unit tests.

Why the name goldie?

The name comes from the fact that it's for Go and handles golden file testing. But yes, it may not be the best name in the world.

How did you come up with the idea?

This is based on the great Advanced Go testing talk by @mitchellh.

License

MIT

Copyright 2016 Sebastian Dahlgren sebastian.dahlgren@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package goldie provides test assertions based on golden files. It's typically used for testing responses with larger data bodies.

The concept is straight forward. Valid response data is stored in a "golden file". The actual response data will be byte compared with the golden file and the test will fail if there is a difference.

Updating the golden file can be done by running `go test -update ./...`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(engine DiffEngine, actual string, expected string) (diff string)

Diff generates a string that shows the difference between the actual and the expected. This method could be called in your own DiffFn in case you want to leverage any of the engines defined.

Types

type DiffEngine

type DiffEngine int

DiffEngine is used to enumerate the diff engine processors that are available.

const (
	// UndefinedDiff represents any undefined diff processor. If a new diff
	// engine is implemented, it should be added to this enumeration and to the
	// `diff` helper function.
	UndefinedDiff DiffEngine = iota

	// ClassicDiff produces a diff similar to what the `diff` tool would
	// produce.
	//		+++ Actual
	//		@@ -1 +1 @@
	//		-Lorem dolor sit amet.
	//		+Lorem ipsum dolor.
	//
	ClassicDiff

	// ColoredDiff produces a diff that will use red and green colors to
	// distinguish the diffs between the two values.
	ColoredDiff

	// Simple is a very plain way of printing the byte comparison. It will look
	// like this:
	//
	// Expected: <data>
	// Got: <data>
	Simple
)

noinspection GoUnusedConst

type DiffFn

type DiffFn func(actual string, expected string) string

DiffFn takes in an actual and expected and will return a diff string representing the differences between the two.

type Goldie added in v2.4.0

type Goldie struct {
	// contains filtered or unexported fields
}

Goldie is the root structure for the test runner. It provides test assertions based on golden files. It's typically used for testing responses with larger data bodies.

func New

func New(t *testing.T, options ...Option) *Goldie

New creates a new golden file tester. If there is an issue with applying any of the options, an error will be reported and t.FailNow() will be called.

func (*Goldie) Assert added in v2.4.0

func (g *Goldie) Assert(t *testing.T, name string, actualData []byte)

Assert compares the actual data received with the expected data in the golden files. If the update flag is set, it will also update the golden file.

`name` refers to the name of the test and it should typically be unique within the package. Also it should be a valid file name (so keeping to `a-z0-9\-\_` is a good idea).

func (*Goldie) AssertJson added in v2.4.0

func (g *Goldie) AssertJson(t *testing.T, name string, actualJsonData interface{})

AssertJson compares the actual json data received with expected data in the golden files. If the update flag is set, it will also update the golden file.

`name` refers to the name of the test and it should typically be unique within the package. Also it should be a valid file name (so keeping to `a-z0-9\-\_` is a good idea).

func (*Goldie) AssertWithTemplate added in v2.4.0

func (g *Goldie) AssertWithTemplate(t *testing.T, name string, data interface{}, actualData []byte)

Assert compares the actual data received with the expected data in the golden files after executing it as a template with data parameter. If the update flag is set, it will also update the golden file. `name` refers to the name of the test and it should typically be unique within the package. Also it should be a valid file name (so keeping to `a-z0-9\-\_` is a good idea).

func (*Goldie) AssertXml added in v2.4.0

func (g *Goldie) AssertXml(t *testing.T, name string, actualXmlData interface{})

AssertXml compares the actual xml data received with expected data in the golden files. If the update flag is set, it will also update the golden file.

`name` refers to the name of the test and it should typically be unique within the package. Also it should be a valid file name (so keeping to `a-z0-9\-\_` is a good idea).

func (*Goldie) GoldenFileName added in v2.4.0

func (g *Goldie) GoldenFileName(t *testing.T, name string) string

GoldenFileName simply returns the file name of the golden file fixture.

func (*Goldie) Update added in v2.4.0

func (g *Goldie) Update(t *testing.T, name string, actualData []byte) error

Update will update the golden fixtures with the received actual data.

This method does not need to be called from code, but it's exposed so that it can be explicitly called if needed. The more common approach would be to update using `go test -update ./...`.

func (*Goldie) WithDiffEngine added in v2.4.0

func (g *Goldie) WithDiffEngine(engine DiffEngine) error

WithDiffEngine sets the `diff` engine that will be used to generate the `diff` text.

func (*Goldie) WithDiffFn added in v2.4.0

func (g *Goldie) WithDiffFn(fn DiffFn) error

WithDiffFn sets the `diff` engine to be a function that implements the DiffFn signature. This allows for any customized diff logic you would like to create.

func (*Goldie) WithDirPerms added in v2.4.0

func (g *Goldie) WithDirPerms(mode os.FileMode) error

WithDirPerms sets the directory permissions for the directories in which the golden files are created.

Defaults to 0755.

func (*Goldie) WithFilePerms added in v2.4.0

func (g *Goldie) WithFilePerms(mode os.FileMode) error

WithFilePerms sets the file permissions on the golden files that are created.

Defaults to 0644.

func (*Goldie) WithFixtureDir added in v2.4.0

func (g *Goldie) WithFixtureDir(dir string) error

WithFixtureDir sets the fixture directory.

Defaults to `testdata`

func (*Goldie) WithIgnoreTemplateErrors added in v2.4.0

func (g *Goldie) WithIgnoreTemplateErrors(ignoreErrors bool) error

WithIgnoreTemplateErrors allows template processing to ignore any variables in the template that do not have corresponding data values passed in.

Default value is false.

func (*Goldie) WithNameSuffix added in v2.4.0

func (g *Goldie) WithNameSuffix(suffix string) error

WithNameSuffix sets the file suffix to be used for the golden file.

Defaults to `.golden`

func (*Goldie) WithSubTestNameForDir added in v2.4.0

func (g *Goldie) WithSubTestNameForDir(use bool) error

WithSubTestNameForDir will create a directory with the sub test's name to store all the golden files. If WithTestNameForDir is enabled, it will be in the test name's directory. Otherwise, it will be in the fixture directory.

Default value is false.

func (*Goldie) WithTestNameForDir added in v2.4.0

func (g *Goldie) WithTestNameForDir(use bool) error

WithTestNameForDir will create a directory with the test's name in the fixture directory to store all the golden files.

Default value is false.

type Option

type Option func(OptionProcessor) error

Option defines the signature of a functional option method that can apply options to an OptionProcessor.

func WithDiffEngine

func WithDiffEngine(engine DiffEngine) Option

WithDiffEngine sets the `diff` engine that will be used to generate the `diff` text.

Default: ClassicDiff noinspection GoUnusedExportedFunction

func WithDiffFn

func WithDiffFn(fn DiffFn) Option

WithDiffFn sets the `diff` engine to be a function that implements the DiffFn signature. This allows for any customized diff logic you would like to create. noinspection GoUnusedExportedFunction

func WithDirPerms

func WithDirPerms(mode os.FileMode) Option

WithDirPerms sets the directory permissions for the directories in which the golden files are created.

Defaults to 0755. noinspection GoUnusedExportedFunction

func WithFilePerms

func WithFilePerms(mode os.FileMode) Option

WithFilePerms sets the file permissions on the golden files that are created.

Defaults to 0644. noinspection GoUnusedExportedFunction

func WithFixtureDir

func WithFixtureDir(dir string) Option

WithFixtureDir sets the fixture directory.

Defaults to `testdata`

func WithIgnoreTemplateErrors

func WithIgnoreTemplateErrors(ignoreErrors bool) Option

WithIgnoreTemplateErrors allows template processing to ignore any variables in the template that do not have corresponding data values passed in.

Default value is false. noinspection GoUnusedExportedFunction

func WithNameSuffix

func WithNameSuffix(suffix string) Option

WithNameSuffix sets the file suffix to be used for the golden file.

Defaults to `.golden`

func WithSubTestNameForDir

func WithSubTestNameForDir(use bool) Option

WithSubTestNameForDir will create a directory with the sub test's name to store all the golden files. If WithTestNameForDir is enabled, it will be in the test name's directory. Otherwise, it will be in the fixture directory.

Default value is false. noinspection GoUnusedExportedFunction

func WithTestNameForDir

func WithTestNameForDir(use bool) Option

WithTestNameForDir will create a directory with the test's name in the fixture directory to store all the golden files.

Default value is false. noinspection GoUnusedExportedFunction

type OptionProcessor

type OptionProcessor interface {
	WithFixtureDir(dir string) error
	WithNameSuffix(suffix string) error
	WithFilePerms(mode os.FileMode) error
	WithDirPerms(mode os.FileMode) error

	WithDiffEngine(engine DiffEngine) error
	WithDiffFn(fn DiffFn) error
	WithIgnoreTemplateErrors(ignoreErrors bool) error
	WithTestNameForDir(use bool) error
	WithSubTestNameForDir(use bool) error
}

OptionProcessor defines the functions that can be called to set values for a tester. To expand this list, add a function to this interface and then implement the generic option setter below.

type Tester

type Tester interface {
	Assert(t *testing.T, name string, actualData []byte)
	AssertJson(t *testing.T, name string, actualJsonData interface{})
	AssertXml(t *testing.T, name string, actualXmlData interface{})
	AssertWithTemplate(t *testing.T, name string, data interface{}, actualData []byte)
	Update(t *testing.T, name string, actualData []byte) error
	GoldenFileName(t *testing.T, name string) string
}

Tester defines the methods that any golden tester should support.

Jump to

Keyboard shortcuts

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