Go 1.9 will ship a Helper()
function defined on *testing.T
so that you can finally write assert helpers which actually point you to the position where the assertion fails! Table based tests are still great!
Test
package helper
import "testing"
func TestHelper(t *testing.T) {
a := &asserter{T: t}
a.Equal(1, 1)
a.Equal(1, 2)
a.Equal(1, 3)
}
type asserter struct {
T *testing.T
}
func (m *asserter) Equal(a, b interface{}) {
m.T.Helper()
if a != b {
m.T.Errorf("expected %#v (%T) was %#v (%T)", a, a, b, b)
}
}
Output
=== RUN TestHelper
--- FAIL: TestHelper (0.00s)
run_test.go:8: expected 1 (int) was 2 (int)
run_test.go:9: expected 1 (int) was 3 (int)
FAIL
exit status 1
FAIL github.com/tobstarr/gohelper 0.001s
Test with
docker run -t -i --rm \
-v $(pwd):/go/src/github.com/tobstarr/gohelper \
golang:1.9beta1-alpine \
go test -v github.com/tobstarr/gohelper