golden

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 9 Imported by: 0

README

Build Status codecov Maintainability

golden - package defines test helper for working with golden files

Golden files contain expected values within your tests. They are useful when you want to check more complex outputs. This package makes it easy to Save and Load such files within the testdata directory.

Simplest example

func TestMe(t *testing.T) {
    got := doSomething()
    golden.Assert(t, got)
}

The golden file for above test is saved in testdata/package.TestMe and an entry is added to testdata/golden.files which keeps track of used files. If you eg. rename a test the golden file will be saved under a new name.

Keep golden.files under revision control to quickly spot which files are no longer used.

To update the golden files use

go test -args -update-golden

Article at 7de.se/golden

Documentation

Overview

Package golden enables reading and writing golden files in testdata.

Within your test, check complex output

func TestMeShort(t *testing.T) {
    complex := doSomething()
    // Does got equal the content of the golden file and update
    // golden file if -update-golden flag is given.
    golden.Assert(t, complex)
}

Golden file is saved in testdata/package.TestMeShort and an entry is added to testdata/golden.files

To update the golden files use

go test -args -update-golden

As test names change over time the testdata/golden.files index is updated but the golden files cannot automatically be renamed or removed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert added in v0.2.0

func Assert(t T, got string)

Assert compares got to the contents of the default golden file found in testdata/ matching the name of the test calling the assert.

Example
package main

import (
	"github.com/gregoryv/golden"
)

func main() {
	got := doSomething()
	// Assert and update if -update-golden flag is given
	golden.Assert(t, got)
}

func doSomething() string { return "hello" }

type noTest struct {
	ok bool
}

func (t *noTest) Errorf(string, ...interface{}) { t.ok = false }
func (t *noTest) Helper()                       {}
func (t *noTest) Fatal(...interface{})          { t.ok = false }

var t *noTest = &noTest{}
Output:

func AssertEquals added in v0.7.0

func AssertEquals(t T, got, exp string)

AssertEquals compares got with exp.

Example
package main

import (
	"github.com/gregoryv/golden"
)

func main() {
	got := doSomething()
	exp := "hello"
	golden.AssertEquals(t, got, exp)
}

func doSomething() string { return "hello" }

type noTest struct {
	ok bool
}

func (t *noTest) Errorf(string, ...interface{}) { t.ok = false }
func (t *noTest) Helper()                       {}
func (t *noTest) Fatal(...interface{})          { t.ok = false }

var t *noTest = &noTest{}
Output:

func AssertWith added in v0.4.0

func AssertWith(t T, got, filename string)

AssertWith compares got with the contents of filename. If -update-golden flag is given got is saved into filename.

Example
package main

import (
	"github.com/gregoryv/golden"
)

var somefile string

func main() {
	got := doSomething()
	golden.AssertWith(t, got, somefile)
}

func doSomething() string { return "hello" }

type noTest struct {
	ok bool
}

func (t *noTest) Errorf(string, ...interface{}) { t.ok = false }
func (t *noTest) Helper()                       {}
func (t *noTest) Fatal(...interface{})          { t.ok = false }

var t *noTest = &noTest{}
Output:

func Load

func Load() []byte

Load returns the content of a stored golden file, defaults to empty slice.

func LoadString

func LoadString() string

LoadString loads the golden string from file using the default store

func Save

func Save(t T, data []byte)

Save saves the data as a golden file using the callers func name

func SaveString

func SaveString(t T, data string)
Example
package main

import (
	"github.com/gregoryv/golden"
)

func main() {
	got := doSomething()
	exp := golden.LoadString()
	if got != exp {
		t.Errorf("Got %q, expected %q", got, exp)
	}
	// Save if -update-golden flag is given
	golden.SaveString(t, got)
}

func doSomething() string { return "hello" }

type noTest struct {
	ok bool
}

func (t *noTest) Errorf(string, ...interface{}) { t.ok = false }
func (t *noTest) Helper()                       {}
func (t *noTest) Fatal(...interface{})          { t.ok = false }

var t *noTest = &noTest{}
Output:

Types

type Store added in v0.5.0

type Store struct {
	RootDir   string
	IndexFile string
	// contains filtered or unexported fields
}

Store defines a location and index of golden files.

var (
	DefaultStore *Store = NewStore()
)

func NewStore added in v0.5.0

func NewStore() *Store

NewStore returns a Store initialized with testdata as RootDir and golden.files as IndexFile

func (*Store) Load added in v0.5.0

func (s *Store) Load() []byte

Load returns body of a golden file based on caller func name. Empty if no golden file exists.

func (*Store) Save added in v0.5.0

func (s *Store) Save(t T, data []byte)

Save writes the given data to RootDir with name from caller func

type T

type T interface {
	Errorf(string, ...interface{})
	Helper()
	Fatal(...interface{})
}

T defines parts of testing.T needed in this package

Jump to

Keyboard shortcuts

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