You can do what you ask, and you can find out how t.Error works by looking at the source code . The decorate function is what you are looking for, I think.
But in the case when you have a significant amount of verification code, and for some reason it gets duplication in your test, it is better to extract it as a function that returns an error than pass it to test.T and make this βstatementβ. Indeed, writing statement functions is clearly not recommended in the FAQ language.
package hello import "testing" func TestFoo(t *testing.T) { if err := checkSomething(2+2 == 4); err != nil { t.Errorf("2+2=4 failed: %s", err) } if err := checkSomething(2+3 == 6); err != nil { t.Errorf("2+3=6 failed: %s", err) } } func checkSomething(v bool) error { if !v { return errors.New("something not right") } return nil }
But here is what I think the idiomatic testing code will look like. It is managed by tables, and in cases include inputs and expected result, which leads to really clear error messages when tests fail.
package hello import "testing" func TestFoo(t *testing.T) { cases := []struct { a, b, want int }{ {2, 2, 4}, {2, 3, 6}, } for _, c := range cases { if got := operation(ca, cb); got != c.want { t.Errorf("operation(%d, %d) = %d, want %d", ca, cb, got, c.want) } } } func operation(a, b int) int { return a + b }