Skip to content

Commit 0c81787

Browse files
author
Dmitry Sinyavskiy
committed
tests: added golden files use and generation example
1 parent 5307f47 commit 0c81787

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

testing/golden-files/golden.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package somepacakge_test
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"io/ioutil"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
// This snippet shows how to create and use golden-files. They helps us to test complex output without manually hardcoding it
12+
// Very scalable way to test complex structures (write a String() method)
13+
//
14+
// = How to use golden files? =
15+
// * generate golden files by running test with `-update-golden` flag (`go test -update-golden`)
16+
// * check manually generated data in a files and if it's correct then commit it.
17+
18+
var updateGolden = flag.Bool("update-golden", false, "update golden files")
19+
20+
func TestAdd(t *testing.T) {
21+
// ... table (probably!)
22+
for _, tc := range сases {
23+
actual := doSomething(tc)
24+
golden := filepath.Join("test-fixtures", tc.Name+".golden")
25+
if *updateGolden {
26+
ioutil.WriteFile(golden, actual, 0644)
27+
}
28+
expected, _ := ioutil.ReadFile(golden)
29+
if !bytes.Equal(actual, expected) {
30+
// FAIL!
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)