From golang-skills
Writes, reviews, and improves Go tests using table-driven tests, subtests, parallel tests, helpers, test doubles, cmp.Diff assertions, and Google/Uber style guides.
npx claudepluginhub cxuu/golang-skills --plugin golang-skillsThis skill is limited to using the following tools:
| Pattern | Use When |
Guides Go test writing, reviews, and audits using table-driven tests, subtests, t.Parallel, t.Helper, t.Cleanup, testify, mocks/stubs, benchmarks, fuzzing.
Guides table-driven tests in Go: when to use/avoid, struct design, subtest naming, test matrices, shared setup, and refactoring bloated tables. Use for writing, reviewing, or refactoring them.
Reviews Go test code for table-driven tests, assertions, parallel execution, cleanup, mocking, benchmarks, fuzzing, httptest, and coverage patterns in *_test.go files.
Share bugs, ideas, or general feedback.
| Pattern | Use When |
|---|---|
t.Error | Default — report failure, keep running |
t.Fatal | Setup failed or continuing is meaningless |
cmp.Diff | Comparing structs, slices, maps, protos |
| Table-driven | Many cases share identical logic |
| Subtests | Need filtering, parallel execution, or naming |
t.Helper() | Any test helper function (call as first statement) |
t.Cleanup() | Teardown in helpers instead of defer |
Normative: Test failures must be diagnosable without reading the test source.
Every failure message must include: function name, inputs, actual (got), and
expected (want). Use the format YourFunc(%v) = %v, want %v.
// Good:
t.Errorf("Add(2, 3) = %d, want %d", got, 5)
// Bad: Missing function name and inputs
t.Errorf("got %d, want %d", got, 5)
Always print got before want: got %v, want %v — never reversed.
Normative: Do not use assertion libraries. Use
cmp.Difffor complex comparisons.
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("GetPost() mismatch (-want +got):\n%s", diff)
}
For protocol buffers, add protocmp.Transform() as a cmp option. Always
include the direction key (-want +got) in diff messages. Avoid comparing
JSON/serialized output — compare semantically instead.
Read references/TEST-HELPERS.md when writing custom comparison helpers or domain-specific test utilities.
Normative: Use
t.Errorby default to report all failures in one run. Uset.Fatalonly when continuing is impossible.
Choose t.Fatal when:
Never call t.Fatal/t.FailNow from a goroutine other than the test
goroutine — use t.Error instead.
Read references/TEST-HELPERS.md when writing helpers that need to choose between t.Error and t.Fatal, or for detailed examples of both.
See
assets/table-test-template.gowhen scaffolding a new table-driven test and need the canonical struct, loop, and subtest layout.
Advisory: Use table-driven tests when many cases share identical logic.
Use table tests when: all cases run the same code path with no conditional
setup, mocking, or assertions. A single shouldErr bool is acceptable.
Don't use table tests when: cases need complex setup, conditional mocking, or multiple branches — write separate test functions instead.
Key rules:
Read references/TABLE-DRIVEN-TESTS.md when writing table-driven tests, subtests, or parallel tests.
Validation: After generating or modifying tests, run
go test -run TestXxx -vto verify the tests compile and pass. Fix any compilation errors before proceeding.
Normative: Test helpers must call
t.Helper()first and uset.Cleanup()for teardown.
func setupTestDB(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Could not open database: %v", err)
}
t.Cleanup(func() { db.Close() })
return db
}
Read references/TEST-HELPERS.md when writing test helpers, cleanup functions, or custom comparison utilities.
Advisory: Test error semantics, not error message strings.
// Bad: Brittle string comparison
if err.Error() != "invalid input" { ... }
// Good: Semantic check
if !errors.Is(err, ErrInvalidInput) { ... }
For simple presence checks when specific semantics don't matter:
if gotErr := err != nil; gotErr != tt.wantErr {
t.Errorf("f(%v) error = %v, want error presence = %t", tt.input, err, tt.wantErr)
}
Read references/TEST-ORGANIZATION.md when working with test doubles, choosing test package placement, or scoping test setup.
Read references/VALIDATION-APIS.md when designing reusable test validation functions.
Read references/INTEGRATION.md when writing TestMain, acceptance tests, or tests that need real HTTP/RPC transports.
scripts/gen-table-test.sh — Generates a table-driven test scaffoldbash scripts/gen-table-test.sh ParseConfig config > config/parse_config_test.go
bash scripts/gen-table-test.sh --parallel ParseConfig config # with t.Parallel()
bash scripts/gen-table-test.sh --output config/parse_config_test.go ParseConfig config
errors.Is/errors.As or sentinel errors