Testing
testpolicy
Checks lifecycle ownership in test helpers.
What it detects
Finds non-entry-point test functions that accept *testing.T or *testing.B
and can return without calling the handle’s Helper method.
Checks
| Check | What it detects |
|---|---|
helper-marker* |
Reports test helpers that do not call Helper on every return path. |
* Opt-in; requires explicit selection.
Why this is flagged
Without t.Helper(), a failure inside a shared test helper points at the
helper itself instead of the test call that caused it. Marking the helper gives
developers a useful file and line when the test fails.
Further reading: testing.T.Helper.
How to fix it
Call t.Helper() near the start of every function that acts as a test helper.
Do it on every path before the helper reports a failure or returns control to
the test.
Examples
Flagged code
// gohawk: test helper accepting t must call t.Helper() on every return pathfunc requireUser(t *testing.T, user *User) { if user == nil { t.Fatal("expected a user") }}Accepted code
func requireUserSafely(t *testing.T, user *User) { t.Helper() if user == nil { t.Fatal("expected a user") }}