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
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
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
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 SaveString ¶
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
Store defines a location and index of golden files.
func NewStore ¶ added in v0.5.0
func NewStore() *Store
NewStore returns a Store initialized with testdata as RootDir and golden.files as IndexFile