All Projects → sebdah → Goldie

sebdah / Goldie

Licence: mit
Golden file testing for Go

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Goldie

fixtures
🔧 Doctrine Fixtures for Nette Framework
Stars: ✭ 15 (-87.6%)
Mutual labels:  fixtures
Model mommy
No longer maintained, please migrate to model_bakery
Stars: ✭ 929 (+667.77%)
Mutual labels:  fixtures
Pytest Deadfixtures
Plugin to list unused fixtures in your tests
Stars: ✭ 89 (-26.45%)
Mutual labels:  fixtures
cypress-xhr-responses-recording
No description or website provided.
Stars: ✭ 19 (-84.3%)
Mutual labels:  fixtures
Django Smuggler
Django Smuggler is a pluggable application for Django Web Framework that helps you to import/export fixtures via the automatically-generated administration interface.
Stars: ✭ 350 (+189.26%)
Mutual labels:  fixtures
Wp Cli Fixtures
Easily generate custom fake data for WordPress
Stars: ✭ 65 (-46.28%)
Mutual labels:  fixtures
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (-79.34%)
Mutual labels:  fixtures
Beanmother
A library for setting up Java objects as test data.
Stars: ✭ 102 (-15.7%)
Mutual labels:  fixtures
Testfixtures
Ruby on Rails like test fixtures for Go. Write tests against a real database
Stars: ✭ 639 (+428.1%)
Mutual labels:  fixtures
Mockwebserverplus
✔️ OkHttp mockwebserver with fixtures extension
Stars: ✭ 87 (-28.1%)
Mutual labels:  fixtures
Mimesis
Mimesis is a high-performance fake data generator for Python, which provides data for a variety of purposes in a variety of languages.
Stars: ✭ 3,439 (+2742.15%)
Mutual labels:  fixtures
Typeorm Fixtures
💊 Fixtures loader for typeorm
Stars: ✭ 330 (+172.73%)
Mutual labels:  fixtures
Babel Test
An opinionated library to make testing babel plugins easier.
Stars: ✭ 79 (-34.71%)
Mutual labels:  fixtures
autofixturejs
genarates random data fixtures for testing
Stars: ✭ 50 (-58.68%)
Mutual labels:  fixtures
Gunit
xUnit-style test fixture adapter for go test
Stars: ✭ 94 (-22.31%)
Mutual labels:  fixtures
lovely-pytest-docker
Pytest plugin providing the ability to use docker-compose services as fixtures.
Stars: ✭ 73 (-39.67%)
Mutual labels:  fixtures
Pytest Mimesis
Mimesis integration with the pytest test runner. This plugin provider useful fixtures based on providers from Mimesis.
Stars: ✭ 46 (-61.98%)
Mutual labels:  fixtures
Narwhal
A progressive test framework for C.
Stars: ✭ 110 (-9.09%)
Mutual labels:  fixtures
Hsac Fitnesse Fixtures
An environment to define and run integration tests. It contains Fitnesse fixture (base) classes and a baseline FitNesse installation.
Stars: ✭ 99 (-18.18%)
Mutual labels:  fixtures
Node Mongodb Fixtures
🍏 Setup and tear down test fixtures with MongoDB.
Stars: ✭ 83 (-31.4%)
Mutual labels:  fixtures

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 [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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].