Skip to content

hansmi/aurum

Repository files navigation

Golden tests in Go

Latest release CI workflow Go reference

The aurum1 package implements golden tests for use in Go unit tests. Values expected from a computation are stored in a file termed "golden file" and differences are reported as test errors. Golden files are only written when requested via a command line flag and if a logical change is detected. Version control is used to track and review changes at the file level.

By default a generic JSON codec is used for storing expected values. The implementation includes special handling of Protocol Buffers which need to be serialized through the protojson package.

String-like data may be stored in plain-text files (TextCodec). When only protocol buffers are compared the textproto codec improves readability over JSON (TextProtoCodec).

Example usage

func init() {
  aurum.Init()
}

func Test(t *testing.T) {
  g := aurum.Golden{
    Dir: "./testdata",
  }
  g.Assert(t, "example", []string{"expected", "value"})
}

A more complete code example can be found in the example directory. To update the golden files:

go test -update_golden_files

Alternatives

Footnotes

  1. Aurum is Latin for gold.