ApprovalTests for go
ApprovalTests allows for easy testing of larger objects, strings and anything else that can be saved to a file (images, sounds, csv, etc...)
func TestHelloWorld(t *testing.T) {
approvals.VerifyString(t, "Hello World!")
}
Some people prefer to store their approved files in a subfolder "testdata" instead of in the same folder as the production code. To configure this, add a call to UseFolder to your TestMain:
func TestMain(m *testing.M) {
UseFolder("testdata")
os.Exit(m.Run())
}
Note: ApprovalTests uses approvals to test itself. Therefore there are many examples in the code itself.
VerifyJSONBytes - Simple Formatting for easy comparison. Also uses the .json file extension
func TestVerifyJSON(t *testing.T) {
jsonb := []byte("{ \"foo\": \"bar\", \"age\": 42, \"bark\": \"woof\" }")
VerifyJSONBytes(t, jsonb)
}
Matches file: approvals_test.TestVerifyJSON.received.json
{
"age": 42,
"bark": "woof",
"foo": "bar"
}
ApprovalTests becomes much more powerful with reporters. Reporters launch programs on failure to help you understand, fix and approve results.
You can make your own easily, here's an example You can also declare which one to use. Either at the
r := UseReporter(reporters.NewIntelliJ())
defer r.Close()
func TestMain(m *testing.M) {
r := UseReporter(reporters.NewBeyondCompareReporter())
defer r.Close()
UseFolder("testdata")
os.Exit(m.Run())
}