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 ¶
- func Diff(engine DiffEngine, actual string, expected string) (diff string)
- type DiffEngine
- type DiffFn
- type EqualFn
- type Goldie
- func (g *Goldie) Assert(t testing.TB, name string, actualData []byte)
- func (g *Goldie) AssertJson(t testing.TB, name string, actualJsonData interface{})
- func (g *Goldie) AssertWithTemplate(t testing.TB, name string, data interface{}, actualData []byte)
- func (g *Goldie) AssertXml(t testing.TB, name string, actualXmlData interface{})
- func (g *Goldie) GoldenFileData(t testing.TB, name string) []byte
- func (g *Goldie) GoldenFileName(t testing.TB, name string) string
- func (g *Goldie) Update(t testing.TB, name string, actualData []byte) error
- func (g *Goldie) UpdateWithTemplate(t testing.TB, name string, data interface{}, actualData []byte) error
- func (g *Goldie) WithDiffEngine(engine DiffEngine) error
- func (g *Goldie) WithDiffFn(fn DiffFn) error
- func (g *Goldie) WithDirPerms(mode os.FileMode) error
- func (g *Goldie) WithEqualFn(fn EqualFn) error
- func (g *Goldie) WithFilePerms(mode os.FileMode) error
- func (g *Goldie) WithFixtureDir(dir string) error
- func (g *Goldie) WithIgnoreTemplateErrors(ignoreErrors bool) error
- func (g *Goldie) WithNameSuffix(suffix string) error
- func (g *Goldie) WithSubTestNameForDir(use bool) error
- func (g *Goldie) WithTestNameForDir(use bool) error
- type Option
- func WithDiffEngine(engine DiffEngine) Option
- func WithDiffFn(fn DiffFn) Option
- func WithDirPerms(mode os.FileMode) Option
- func WithEqualFn(fn EqualFn) Option
- func WithFilePerms(mode os.FileMode) Option
- func WithFixtureDir(dir string) Option
- func WithIgnoreTemplateErrors(ignoreErrors bool) Option
- func WithNameSuffix(suffix string) Option
- func WithSubTestNameForDir(use bool) Option
- func WithTestNameForDir(use bool) Option
- type OptionProcessor
- type Tester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DiffEngine ¶
type DiffEngine int
DiffEngine is used to enumerate the diff engine processors that are available.
const ( // UndefinedDiff represents any undefined diff processor. If a new diff // engine is implemented, it should be added to this enumeration and to the // `diff` helper function. UndefinedDiff DiffEngine = iota // ClassicDiff produces a diff similar to what the `diff` tool would // produce. // +++ Actual // @@ -1 +1 @@ // -Lorem dolor sit amet. // +Lorem ipsum dolor. // ClassicDiff // ColoredDiff produces a diff that will use red and green colors to // distinguish the diffs between the two values. ColoredDiff // Simple is a very plain way of printing the byte comparison. It will look // like this: // // Expected: <data> // Got: <data> Simple )
noinspection GoUnusedConst
type DiffFn ¶
DiffFn takes in an actual and expected and will return a diff string representing the differences between the two.
type Goldie ¶ added in v2.4.0
type Goldie struct {
// contains filtered or unexported fields
}
Goldie is the root structure for the test runner. It provides test assertions based on golden files. It's typically used for testing responses with larger data bodies.
func New ¶
New creates a new golden file tester. If there is an issue with applying any of the options, an error will be reported and t.FailNow() will be called.
func (*Goldie) Assert ¶ added in v2.4.0
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 (*Goldie) AssertJson ¶ added in v2.4.0
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 (*Goldie) AssertWithTemplate ¶ added in v2.4.0
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 (*Goldie) AssertXml ¶ added in v2.4.0
AssertXml compares the actual xml 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 (*Goldie) GoldenFileData ¶ added in v2.8.0
GoldenFileData returns the data from the requested golden fixture 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 (*Goldie) GoldenFileName ¶ added in v2.4.0
GoldenFileName simply returns the file name of the golden file fixture.
func (*Goldie) Update ¶ added in v2.4.0
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 ./...` or `GOLDIE_UPDATE=true go test ./...`.
func (*Goldie) UpdateWithTemplate ¶ added in v2.7.0
func (g *Goldie) UpdateWithTemplate(t testing.TB, name string, data interface{}, actualData []byte) error
UpdateWithTemplate will update the golden fixtures with the received actual data, replacing any values in the actual data with template variables that match the values in the data structure.
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 ./...` or `GOLDIE_UPDATE=true go test ./...`.
func (*Goldie) WithDiffEngine ¶ added in v2.4.0
func (g *Goldie) WithDiffEngine(engine DiffEngine) error
WithDiffEngine sets the `diff` engine that will be used to generate the `diff` text.
func (*Goldie) WithDiffFn ¶ added in v2.4.0
WithDiffFn sets the `diff` engine to be a function that implements the DiffFn signature. This allows for any customized diff logic you would like to create.
func (*Goldie) WithDirPerms ¶ added in v2.4.0
WithDirPerms sets the directory permissions for the directories in which the golden files are created.
Defaults to 0755.
func (*Goldie) WithEqualFn ¶ added in v2.5.4
WithEqualFn sets the customized equality comapre function that implements the EqualFn signature.
func (*Goldie) WithFilePerms ¶ added in v2.4.0
WithFilePerms sets the file permissions on the golden files that are created.
Defaults to 0644.
func (*Goldie) WithFixtureDir ¶ added in v2.4.0
WithFixtureDir sets the fixture directory.
Defaults to `testdata`
func (*Goldie) WithIgnoreTemplateErrors ¶ added in v2.4.0
WithIgnoreTemplateErrors allows template processing to ignore any variables in the template that do not have corresponding data values passed in.
Default value is false.
func (*Goldie) WithNameSuffix ¶ added in v2.4.0
WithNameSuffix sets the file suffix to be used for the golden file.
Defaults to `.golden`
func (*Goldie) WithSubTestNameForDir ¶ added in v2.4.0
WithSubTestNameForDir will create a directory with the sub test's name to store all the golden files. If WithTestNameForDir is enabled, it will be in the test name's directory. Otherwise, it will be in the fixture directory.
Default value is false.
func (*Goldie) WithTestNameForDir ¶ added in v2.4.0
WithTestNameForDir will create a directory with the test's name in the fixture directory to store all the golden files.
Default value is false.
type Option ¶
type Option func(OptionProcessor) error
Option defines the signature of a functional option method that can apply options to an OptionProcessor.
func WithDiffEngine ¶
func WithDiffEngine(engine DiffEngine) Option
WithDiffEngine sets the `diff` engine that will be used to generate the `diff` text.
Default: ClassicDiff noinspection GoUnusedExportedFunction
func WithDiffFn ¶
WithDiffFn sets the `diff` engine to be a function that implements the DiffFn signature. This allows for any customized diff logic you would like to create. noinspection GoUnusedExportedFunction
func WithDirPerms ¶
WithDirPerms sets the directory permissions for the directories in which the golden files are created.
Defaults to 0755. noinspection GoUnusedExportedFunction
func WithEqualFn ¶ added in v2.5.4
WithEqualFn sets the customized equality comapre function that implements the EqualFn signature. noinspection GoUnusedExportedFunction
func WithFilePerms ¶
WithFilePerms sets the file permissions on the golden files that are created.
Defaults to 0644. noinspection GoUnusedExportedFunction
func WithIgnoreTemplateErrors ¶
WithIgnoreTemplateErrors allows template processing to ignore any variables in the template that do not have corresponding data values passed in.
Default value is false. noinspection GoUnusedExportedFunction
func WithNameSuffix ¶
WithNameSuffix sets the file suffix to be used for the golden file.
Defaults to `.golden`
func WithSubTestNameForDir ¶
WithSubTestNameForDir will create a directory with the sub test's name to store all the golden files. If WithTestNameForDir is enabled, it will be in the test name's directory. Otherwise, it will be in the fixture directory.
Default value is false. noinspection GoUnusedExportedFunction
func WithTestNameForDir ¶
WithTestNameForDir will create a directory with the test's name in the fixture directory to store all the golden files.
Default value is false. noinspection GoUnusedExportedFunction
type OptionProcessor ¶
type OptionProcessor interface {
WithFixtureDir(dir string) error
WithNameSuffix(suffix string) error
WithFilePerms(mode os.FileMode) error
WithDirPerms(mode os.FileMode) error
WithEqualFn(fn EqualFn) error
WithDiffEngine(engine DiffEngine) error
WithDiffFn(fn DiffFn) error
WithIgnoreTemplateErrors(ignoreErrors bool) error
WithTestNameForDir(use bool) error
WithSubTestNameForDir(use bool) error
}
OptionProcessor defines the functions that can be called to set values for a tester. To expand this list, add a function to this interface and then implement the generic option setter below.
type Tester ¶
type Tester interface {
Assert(t testing.TB, name string, actualData []byte)
AssertJson(t testing.TB, name string, actualJsonData interface{})
AssertXml(t testing.TB, name string, actualXmlData interface{})
AssertWithTemplate(t testing.TB, name string, data interface{}, actualData []byte)
Update(t testing.TB, name string, actualData []byte) error
GoldenFileName(t testing.TB, name string) string
GoldenFileData(t testing.TB, name string) []byte
}
Tester defines the methods that any golden tester should support.