When using testify with table-driven tests as in:
func TestFoo(t *testing.T) {
a := assert.New(t)
tc := []struct {
desc string
foo string
}{
{
desc: "fail abc",
foo: "abc",
},
{
desc: "fail def",
foo: "def",
},
}
for _, tC := range tc {
t.Run(tC.desc, func(t *testing.T) {
tC := tC
a.Equal(tC.foo, "ghi")
})
}
}
Your failures will come back ambiguous:
--- FAIL: TestFoo (0.00s)
/test.go:27:
Error Trace: /test.go:27
Error: Not equal:
expected: "abc"
actual : "ghi"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-abc
+ghi
Test: TestFoo
/test.go:27:
Error Trace: /test.go:27
Error: Not equal:
expected: "def"
actual : "ghi"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-def
+ghi
Test: TestFoo
FAIL
Rather than telling you which subtest failed, they all show the parent test having failed (e.g. TestFoo
instead of TestFoo/fail_abc
). If there are many tests in the table, this can make the test completely useless.