Skip to content

Instantly share code, notes, and snippets.

@tidwall
Created January 16, 2017 14:37
Show Gist options
  • Save tidwall/e3ba31c7e61cd042180078609d4d1d7e to your computer and use it in GitHub Desktop.
Save tidwall/e3ba31c7e61cd042180078609d4d1d7e to your computer and use it in GitHub Desktop.
Golang Testing Helpers
func testOrDie(t testing.TB, fn func() error) {
if err := fn(); err != nil {
testFatal(t, 1, err)
}
}
func testFatalf(t testing.TB, skip int, format string, args ...interface{}) {
if _, file, line, ok := runtime.Caller(skip + 1); ok {
msg := fmt.Sprintf(format, args...)
t.Fatalf("\r\t%s:%d: %s", filepath.Base(file), line, msg)
}
t.Fatalf(format, args...)
}
func testFatal(t testing.TB, skip int, args ...interface{}) {
testFatalf(t, skip+1, "%s", fmt.Sprint(args...))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment