Guides writing, fixing, and migrating Go tests using the gotest framework (github.com/mvrahden/go-test). Covers *TestSuite structs, gotest.T, hooks, fixtures, CLI usage, and CI setup.
Ships in gotest. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
gotest inverts habits learned from stdlib/testify. Follow this file's rules;
consult reference/ only when the task touches that area — except
reference/setup.md, which every task follows before writing anything.
The repo has the library; the CLI runs via Go's tool directive, so every
command is go tool gotest <args> — never a global go install binary.
Before writing anything, check the pin and what the module can take:
go list -m -u -f '{{.Version}} {{with .Update}}{{.Version}}{{end}} {{with .Replace}}(replaced){{end}}' github.com/mvrahden/go-test
go list -m -f '{{.GoVersion}}'
go list -m -f '{{.Version}} {{.GoVersion}}' $(go list -m -versions github.com/mvrahden/go-test | tr ' ' '\n' | sed -n 's|^v|github.com/mvrahden/go-test@v|p')
The first line prints the pin (and a newer release, if any); the second
the module's go line; the third every release with its go line. Upgrade to
the newest release whose go line is not newer than the module's, when it
is newer than the pin —
go get -tool github.com/mvrahden/go-test/cmd/gotest@<release> — never to
@latest blindly: go get would silently raise the project's Go. Do not
upgrade when the pin is (replaced) (a source build: assume current
behavior), the module is vendored, instructions rule out dependency
changes, the task only reads code, or the check fails. Then follow
reference/setup.md (after-upgrade steps, keeping the pin, a module
without go-test yet, go.work, vendoring). A FAIL: refusal about a
drifted binary is never a fault in the tests: switch to go tool gotest;
never upgrade just to silence it.
This skill describes v1.42, the newest release; a rule tagged with a
later version waits for that release. On a release you could not upgrade from,
read reference/versions.md before writing anything: it lists what each
older release lacks or does differently, including rules below that fail
there (on v1.25.x, rules 2, 3 and 10). A rule tagged v1.27+ (or later)
needs that release; skip it on older ones.
go test ignores gotest suites (signature incompatibility, by design).
gotest runs suites and never runs stdlib tests — it prints a stderr
note: N stdlib test(s) … not run when it skips them (and [no suites]
for a package holding nothing else). Running only one command silently
misses tests. Always finish with:
go tool gotest ./...
go test ./...
Add -race to both before calling anything done.
Under an agent (AI_AGENT, or CLAUDECODE=1) a text run prints for you
(v1.42+): only what failed, then one closing line, last on stdout, that
survives tail -1: gotest: PASS|FAIL|EMPTY exit <code> | tests: … | packages: … | rerun: <command>. Read the verdict and exit code from that
line, never from the absence of FAIL; a suite that ran none of its tests
fails the run with exit 2. go tool gotest --failed reruns only what the
last run in this directory failed, with -json, spec and summary too,
and exits 2 when nothing is recorded to rerun, 1 when a recorded failure names a test that is gone. --agent=false or
GOTEST_AGENT=0 gives you go test's text instead. While you iterate,
go tool gotest --changed ./... (v1.42+) runs only the packages whose
sources changed since the last run here, or that failed in it; its closing
line says how many it left out, so the final check still runs everything.
Before you report a change done, audit it against its base (v1.42+):
go tool gotest audit --base=$(git merge-base origin/main HEAD) reads both
trees' suites from source and lists every weakening before the behaviors
added, removed, reworded, moved and changed: a test removed, an X_ or
F_ added, a guard, a raised budget or NoDeadline, a new suppression, a
rewritten snapshot, fewer assertions, a new AssertsBy. Each one is what
the change meant, said in your report, or a mistake to undo. Add --run to
run what changed: each behavior then shows its verdict, and the changed
production lines no test executed are listed. When the change adds logic,
add --mutate (v1.42+), experimental, within its budget: it makes each
changed line a test executed wrong and reports the lines no test noticed.
For a survivor,
write the assertion that tells the mutant apart; only a mutant no test can
tell apart, such as a heuristic's equally valid result, takes
//gotest:mutate-ignore <the true reason>, which audit lists as a
suppression. The gotest plugin's stop hook
runs the same audit when you end a turn and hands you each new weakening
once: undo it, or name it in your final message.
When the repository keeps OpenSpec scenarios (specs/ or openspec/specs/),
bind each one you implement by its text (v1.42+): a t.When whose label is
the scenario's WHEN and, directly under it, a t.It per THEN and AND,
copied word for word. go tool gotest scenarios --verify then names every
scenario no suite states, every clause two tests claim, and the verdict of
each bound one; rewording a scenario means rewording its labels too.
Write → lint → both runners → fix:
go tool gotest lint -fix ./...
go tool gotest lint ./...
lint -fix applies every suggested fix, formats the files it rewrites and
removes the imports its fixes left unused, so no gofmt or goimports pass
is needed after it. Findings without a fix (t.T().Helper(), a
t.T().Log, a stdlib test, …) need your edit: from v1.33 -fix lists them
and exits 3; below, it exits 0 and prints nothing, so only the plain
lint run shows them. Move on when plain lint exits 0.
The linter catches direct misuse (t.T() escapes even inside closures,
outer-t in poll callbacks, testify idioms, focus leftovers, and
fail-guard: any if cond { gotest.Fail(t, …) } or if err != nil { t.Fatal(err) } guard — assertions halt on failure, so state them
directly: gotest.NoError(t, err), never a guarded fail). A test
asserts, or says how it checks (v1.42+): a leaf that passes having made no
assertion is vacuous, and --ci fails the run on it. Add the missing
assertion; only a test that truly checks by other means (code given
t.T(), a call that must not panic) declares it with
t.AssertsBy("<how>"). Fixes can
compose — a rewritten guard may itself be simplifiable; from v1.33 one
-fix applies those too, below it re-run the pair until plain lint is
clean. When two findings share a construct
the linter reports only the stronger one (integrity over style); fix what
it says before expecting style suggestions there.
Suppression follows rule tiers: integrity rules (poll-scope,
suite-lifecycle, focus, …) can never be skipped project-wide; style
and migration rules can, via .gotest.yml lint.skip or -skip-<rule>
flags. Any rule yields to a //nolint:<rule> // <reason> comment on the
finding's line or on a line of its own directly above it — and to one
trailing the package line itself
(package shop_test //nolint:stdlib-test // <reason>), which
covers the whole FILE (not the package), whatever the tier; on the line
above package it suppresses nothing. Write the reason (v1.42+:
nolint-reason reports a comment without one, and gotest lint -suppressions lists every comment with its reason for review). The
stdlib-test rule flags every stdlib TestXxx(*testing.T) (not Benchmark, Fuzz or Example
functions — those show only in the runner's note) — when a stdlib test is
intentional (assertion-layer tests, benchmarks-adjacent code), mark it
that way, or the lint run fails. The linter does NOT catch: plain defer
cleanup, shared mutable state in parallel suites, or structural problems —
those are your job, below.
Suites are structs, naming is the API. type XxxTestSuite struct{},
exported, methods func (s *X) TestBehavior(t *gotest.T). No
registration and no TestMain needed — the CLI generates the harness
invisibly (never commit the gotest_psuite_test.go/
gotest_pxsuite_test.go files gotest generate writes; gotest clean ./...
removes them). A TestMain of your own still runs; keep resource setup
out of it — that is what fixtures and BeforeAll are for. F_/X_
prefixes focus/exclude. A suite declares at least one Test*,
Benchmark* or Fuzz* method (an X_ one counts); a *TestSuite
type with none is refused, so name helper types otherwise (v1.37+).
A generic suite runs only through one
type alias per concrete type
(type ArticleIndexTestSuite = IndexContractTestSuite[Article]);
without an alias it never runs, and the run refuses it (v1.38+).
Case-named methods taking *gotest.T/B/F on a test-file type
that is no suite are refused unless a test file calls them, and so is
a method named just Test, Benchmark or Fuzz (v1.38+).
Lifecycle hooks own resources. Setup in BeforeEach (or BeforeAll
for expensive read-only state), teardown in AfterEach/AfterAll —
per-test state on suite fields in a sequential suite, in the returned
context in a parallel one (rule 4). NEVER t.T().Cleanup, and no
defer releasing a resource in a test method. Such a defer lints
clean and is still wrong: it sits outside the teardown order the
harness guarantees, and the setup cannot be shared or hoisted. Start
goroutines with gotest.Go(t, fn), never a bare go: a panic on a
bare goroutine kills the process before any teardown runs.
wait := gotest.Go(…); defer wait() is the one defer that belongs in
a test, and only for work that finishes on its own — never wait in the
test on a server or poller that AfterEach stops (it deadlocks).
(v1.34+) A goroutine BeforeAll starts for AfterAll to stop goes on
the hook's t or a T derived from it (When, It, Each,
NewTWithDeadline; gotest.NewT(t) in a *testing.T hook), never on
a subtest the hook starts with t.Run: its wait runs before AfterAll
and deadlocks. Below v1.34 every such wait did.
Every BeforeAll needs an AfterAll (lifecycle-pair, integrity
tier). A suite's AfterAll runs even when its BeforeAll failed or
panicked midway, so release only what was actually set up — nil-check
each field.
Do not imitate existing tests blindly. Observed failure: agents copy
a repo's existing SuiteConfig() verbatim, propagating anti-patterns. A
SuiteConfig() marker states intent: omit it entirely for defaults.
Every positive duration the marker returns — a preset's included — is a
budget: a test that outlives it fails even when it ignores its context.
A duration it leaves at zero gets the default and, like having no
marker, only bounds the context; gotest.NoDeadline disables one. On
v1.26–v1.29 a zero/omitted duration meant NO deadline instead — compose
onto a preset there (reference/config.md).
Ask "why is this suite NOT parallel?" Observed failure: agents never parallelize unprompted, even when asked to improve tests. The recipe:
func (s *ShopTestSuite) SuiteConfig() gotest.SuiteConfig {
return gotest.SuiteConfig{Parallel: true}
}
type shopCtx struct{ inv *Inventory }
func (s *ShopTestSuite) BeforeEach(t *gotest.T) *shopCtx {
return &shopCtx{inv: NewInventory()}
}
(v1.26–v1.29: that literal means no deadline at all — compose onto
gotest.DefaultSuiteConfig() there, see reference/config.md.)
Every test method then takes (t *gotest.T, ctx *shopCtx), and an
AfterEach takes (t, ctx) too — the generator enforces this, and
rejects a BeforeEach that returns nothing in a parallel suite. A suite
without fields (or with fixture pointers only) may be parallel with no
BeforeEach. The generator cannot see methods writing suite fields —
with no BeforeEach, or beside a returning one: that compiles, and
races. A suite with Benchmark* or Fuzz* methods refuses a returning
BeforeEach (and hooks taking *testing.T): move those methods to a
sequential sibling suite. It also refuses a hook calling t.T() where
that is nil — every hook under a benchmark, BeforeAll/AfterAll under
a fuzz target — unless the hook checks it for nil; a helper the hook
calls is not inspected, so keep t.T() out of those helpers.
A parallel suite with two or more methods also refuses a bound package
fixture whose BeforeEach/AfterEach uses the fixture (v1.38+): do
that per-method work in the returning BeforeEach.
Legitimate reasons to stay sequential: Setenv in a method or a
per-method hook (panics in parallel tests; refused at generation,
v1.38+; BeforeAll may call it), shared live resources without per-test
keys/schemas (-race is process-local and cannot see datastore
contention — it is necessary, not sufficient).
Poll, never sleep. gotest.Eventually(t, waitFor, tick, func(poll *gotest.R) { ... }) — assert a stable fixed point, not a transient
state. The callback's poll handle has only Errorf/FailNow/
Failed/Message; pass poll (not t) to assertions inside it.
Code whose verdict reads the clock in-process (a backoff, ticker, TTL,
debounce, rate limiter or context deadline) goes in a
gotest.Bubble(t, func(bt *gotest.T) { ... }) instead (v1.42+): it
runs on testing/synctest's fake clock, so an hour passes at once and the
same under load. Assert on bt, gotest.Settle(bt) before reading
what another goroutine wrote, and open no It inside it. Never bubble a
child process, real network I/O or a shared fixture: their time is the
wall clock's (reference/api.md, Fake time).
Never call t.T().Helper() — call sites resolve automatically; the
linter flags it. Reach for t.T() only when nothing on gotest.T
(It, When, Context, TempDir, Setenv, Name, Skipf,
Errorf, FailNow) covers the need — never for t.T().Context(), which carries
no suite Timeout (v1.33+: t-escape flags it, -fix writes
t.Context()). There is no Log: context goes into an assertion's
message arguments (gotest.Equal(t, want, got, "order %s", id)), and
t-escape flags t.T().Log. b.B() and f.F() follow the same rule:
reach for them only for what gotest.B/gotest.F lack (N, timers,
Run on *testing.B); t-escape flags the rest (v1.37+).
Ask "why is this wall-clock-asserting suite NOT Exclusive?"
(v1.27+) — the counterpart to rule 4. A suite whose assertions or
timeout budgets measure elapsed time (latency bounds, timing budgets,
contended ports/containers) cannot share a saturated machine: mark it
SuiteConfig{Exclusive: true} (statically parsed like Parallel —
boolean literal only; see reference/config.md) and it dispatches
strictly alone after all other suites finish. A budget verdict taken
under load is not a verdict you can act on.
Write the condition, not the connective (v1.29+). t.When("email is valid"), never t.When("when email is valid"); t.It("creates the user"), never t.It("it creates the user"). The spec renders every
When label as when <condition> on every surface (terminal, JSON,
discovery, the editor's tree and panel) and the ✓/✗ glyph plays
the role of "it", so the word in the source is said twice. A When
that opens with its own connective (with …, given …, after …,
if …, …) is rendered as written. The behavior-wording rule flags
the redundant word and lint -fix drops it. Subtest names never
carry the connective — -run filters and snapshot keys are unaffected.
Fuzz targets are suite methods, and they assert a property
(v1.29+). func (s *XTestSuite) FuzzParse(f *gotest.F): f.Add
typed seeds first, then f.Fuzz(func(t *gotest.T, in …) { … }) whose
body asserts something the input must satisfy (round-trip, idempotence,
no panic is not enough — fuzz-no-oracle). Never a top-level
func FuzzX(*testing.F): gotest ignores it on every version (the one
exception, a target that must also run under plain go test -fuzz, is
in reference/fuzzing.md). Struct and named-type arguments are fine
and fan out per field; the refused
shapes and the crasher loop are in reference/fuzzing.md. Search with
go tool gotest fuzz --for=30s ./...; a crasher becomes a seed through
gotest fuzz promote right after that session (v1.36.1+: promote takes
only the crashers the latest session of their target wrote), never a hand-committed
corpus file for a struct target. Benchmarks are suite methods too:
func (s *XTestSuite) BenchmarkParse(b *gotest.B) with the measured
work inside for b.Loop() { … } (reference/cli.md).
Gate on the environment with SuiteGuard, never a skip. A suite
that needs what a machine may lack (a DATABASE_URL, Docker, a
credential) declares func (s *X) SuiteGuard() string: empty runs the
suite, anything else skips it with that reason. It runs before the
suite's package fixtures (v1.32+; see reference/versions.md), config
and BeforeAll, so the suite's own setup never starts; t.T().Skip()
in BeforeAll runs after setup has begun. Read the environment only:
fixture fields are still nil in the guard. A shared fixture the suite
binds is started by the runner regardless — no guard can stop it.
When gotest — or this skill — seems to misbehave, first check it really is
gotest's fault (most look-alikes are a stray binary or an old pin), then
draft a report and send it only on the user's yes: follow
reference/reporting.md. Never hide it behind a weakened or deleted test.
Enter only at a green pause point when: setup is duplicated across tests, a
third similar test is being added, a touched suite already smells, or you
were asked to clean up. Follow reference/refactoring.md for the ladder and
smells list. Non-negotiable safety invariants (tests protect nothing —
observed failure: agents restructure with no case accounting):
Capture the executed case LIST before and after, into separate files, and diff them:
go tool gotest -json ./... > run-before.json
go test -json ./... >> run-before.json
grep -o '"Package":"[^"]*","Test":"[^"]*"' run-before.json | sort -u > cases-before.txt
test -s cases-before.txt
Both runs must exit 0 — a run that failed partway still leaves a non-empty list.
Capture both runners (stdlib tests run only under go test) and the
Package+Test PAIR — bare Test names collapse identically named suites
across packages, hiding whole-package deletions.
After the refactor, capture cases-after.txt the same way and run
diff cases-before.txt cases-after.txt. Enumerate every rename/merge
BEFORE editing; every diff line must map to that list. Coverage may
only grow.
Both runners + -race green before AND after.
Never delete or weaken an assertion without saying so in your report.
Never touch production code during a test refactor — a test that resists restructuring is a design finding to report.
Consider rule 4 (parallelization) part of every improvement pass.
package shop_test
import (
"example.com/shop"
"github.com/mvrahden/go-test/pkg/gotest"
)
type CartTestSuite struct {
cart *shop.Cart
}
func (s *CartTestSuite) BeforeEach(t *gotest.T) {
s.cart = shop.NewCart()
}
func (s *CartTestSuite) TestTotalsItems(t *gotest.T) {
s.cart.Add("apple", 2)
gotest.Equal(t, 2, s.cart.Count("apple"))
}
reference/api.md — signatures and rules: assertions, polling, goroutines,
suites, lifecycle, config, fixtures, T/B/F methodsreference/assertions.md — full assertion surface, Nil/NotNil type
guards, snapshot testingreference/setup.md — declaring, checking and upgrading the CLI; drift
refusals, vendored modules, go.workreference/config.md — config semantics, budgets, presets, compose formreference/fixtures.md — fixture DAG, shared fixtures, hooksreference/cli.md — the full CLI surface and flagsreference/refactoring.md — restructuring ladder, smells → movesreference/ci.md — CI workflow shape, the gotest action, linter coexistencereference/migration.md — testify/stdlib → gotestreference/fuzzing.md — fuzz targets, seeds, struct arguments, the crasher loopreference/versions.md — what older releases lack or do differentlyreference/reporting.md — telling a gotest bug from a look-alike, and reporting itreference/api.md (opens in a new tab)reference/assertions.md (opens in a new tab)reference/ci.md (opens in a new tab)reference/cli.md (opens in a new tab)reference/config.md (opens in a new tab)reference/fixtures.md (opens in a new tab)reference/fuzzing.md (opens in a new tab)reference/migration.md (opens in a new tab)reference/refactoring.md (opens in a new tab)reference/reporting.md (opens in a new tab)reference/setup.md (opens in a new tab)reference/versions.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
/gotest:writing-gotest-testsfrom gotest
npx claudepluginhub mvrahden/go-test --plugin gotestOther plugins' skills for the same job