Giter VIP home page Giter VIP logo

goldie's Introduction

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

    g := goldie.New(t)
    g.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 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 ./...

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"))
}

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 [email protected]

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.

goldie's People

Contributors

almalki avatar eripa avatar gtrevg avatar jakdept avatar sebdah avatar xorcare avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.