goldie

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 9 Imported by: 0

README

goldie - Golden test utility for Go

GoDoc CircleCI 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.

Example usage

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()

    goldie.Assert(t, "example", recorder.Body.Bytes())
}

Using template golden file

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 goldie.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",
    }

    goldie.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 ./...

FAQ

Do you need any help in the project?

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

  • Unit tests.
  • Better output for failed tests. A diff of some sort would be great.
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

View Source
var (
	// FixtureDir is the folder name for where the fixtures are stored. It's
	// relative to the "go test" path.
	FixtureDir = "fixtures"

	// FileNameSuffix is the suffix appended to the fixtures. Set to empty
	// string to disable file name suffixes.
	FileNameSuffix = ".golden"

	// FlagName is the name of the command line flag for go test.
	FlagName = "update"

	// FilePerms is used to set the permissions on the golden fixture files.
	FilePerms os.FileMode = 0644

	// DirPerms is used to set the permissions on the golden fixture folder.
	DirPerms os.FileMode = 0755
)

Functions

func Assert

func 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 AssertJson

func 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 AssertWithTemplate

func 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 Update

func Update(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 ./...`.

Types

This section is empty.

Jump to

Keyboard shortcuts

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