From go-agent-skills
Go testing patterns for production-grade code: subtests, test helpers, fixtures, golden files, httptest, testcontainers, property-based testing, and fuzz testing. Covers mocking strategies, test isolation, coverage analysis, and test design philosophy. Use when writing tests, improving coverage, reviewing test quality, setting up test infrastructure, or choosing a testing approach. Trigger examples: "add tests", "improve coverage", "write tests for this", "test helpers", "mock this dependency", "integration test", "fuzz test". Do NOT use for performance benchmarking methodology (use go-performance-review), security testing (use go-security-audit), or table-driven test patterns specifically (use go-test-table-driven).
How this skill is triggered — by the user, by Claude, or both
Slash command
/go-agent-skills:go-test-qualityThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Tests are production code. They run in CI on every commit, they document behavior,
Tests are production code. They run in CI on every commit, they document behavior, and they're the first thing you read when a function breaks at 3am. Write them with the same care you'd give to code that handles money.
Detailed reference material, loaded on demand:
references/helpers-and-fixtures.md — test helpers, factory functions
with options, t.Cleanup, golden files, mock implementations.references/integration-testing.md — httptest recorder and server,
testcontainers, build tags, TestMain, fuzz testing.Read a reference file only when the summary below is not enough.
// ✅ Good — tests what the function DOES
func TestTransferFunds_InsufficientBalance(t *testing.T) {
from := NewAccount("alice", 100)
to := NewAccount("bob", 0)
err := TransferFunds(from, to, 150)
require.ErrorIs(t, err, ErrInsufficientFunds)
assert.Equal(t, 100, from.Balance(), "sender balance should be unchanged")
assert.Equal(t, 0, to.Balance(), "receiver balance should be unchanged")
}
// ❌ Bad — tests HOW the function does it
// asserts debit() was called before credit(), rollback() was called,
// internal mutex was locked — breaks on every refactor
Multiple assert calls are fine when they verify different facets of the
SAME behavior (both accounts after a transfer). A test that checks creation
AND update AND deletion is three tests pretending to be one.
When the test fails, the name alone should say what broke:
// ✅ Good — reads like a sentence
func TestOrderService_Cancel_RefundsPartiallyShippedItems(t *testing.T) { ... }
func TestParseConfig_ReturnsErrorOnMissingRequiredField(t *testing.T) { ... }
// ❌ Bad — says nothing useful
func TestCancel(t *testing.T) { ... }
func TestRateLimiter_Success(t *testing.T) { ... }
Use t.Run to group related scenarios under a parent test. Each subtest
gets its own setup, its own failure, and its own name in CI output:
func TestUserService_Create(t *testing.T) {
svc := setupUserService(t)
t.Run("succeeds with valid input", func(t *testing.T) {
user, err := svc.Create(ctx, CreateUserInput{Name: "Alice", Email: "[email protected]"})
require.NoError(t, err)
assert.NotEmpty(t, user.ID)
})
t.Run("rejects duplicate email", func(t *testing.T) {
_, _ = svc.Create(ctx, CreateUserInput{Name: "Alice", Email: "[email protected]"})
_, err := svc.Create(ctx, CreateUserInput{Name: "Bob", Email: "[email protected]"})
require.ErrorIs(t, err, ErrDuplicateEmail)
})
}
t.Helper() in test utilities so failures point to the
caller, not the helper.t.Cleanup over defer — it runs even after t.FailNow()
and is scoped to the test, not the function.Full implementations in references/helpers-and-fixtures.md.
| Situation | Approach | Details |
|---|---|---|
| Pure function, 3+ data cases | Table-driven test | go-test-table-driven skill |
| HTTP handler in isolation | httptest.NewRecorder + mock store | references/integration-testing.md |
| Full routing/middleware stack | httptest.NewServer | references/integration-testing.md |
| Real database behavior | testcontainers + build tags | references/integration-testing.md |
| Complex output (JSON, HTML, SQL) | Golden files in testdata/ | references/helpers-and-fixtures.md |
| Parser/validator on untrusted input | Fuzz test | references/integration-testing.md |
now func() time.Time).func TestSlugify(t *testing.T) {
t.Parallel() // safe: pure function, no shared state
// ...
}
Do NOT use t.Parallel() when tests share mutable state, databases,
files, or process-level state (os.Setenv).
go test -race -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
Targets: business logic 80%+, critical paths (auth, payments) 95%+, handlers 70%+. Don't chase 100% on generated code and simple getters.
time.Sleep for synchronization — use channels or pollingTest1, TestSuccess — name the scenariot.Helper() called in every test utility functiont.Cleanup() used for resource teardownt.Parallel() used where safe, avoided where nottesting.Short() or build tagsgo test -race ./... passesnpx claudepluginhub eduardo-sl/go-agent-skillsGuides creation of production-ready Go tests: table-driven tests, testify, parallel tests, fuzzing, fixtures, goroutine leak detection, snapshot testing, code coverage, integration tests.
Go testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and TDD methodology for writing reliable tests.
Reviews Go test code for table-driven tests, assertions, parallel execution, cleanup, mocking, benchmarks, fuzzing, httptest, and coverage patterns in *_test.go files.