go-testing
Skill9 starsChanged 4 months agoMITView on GitHub
Use when writing or fixing Go tests — table-driven cases, parallel safety, helpers, fakes, fuzzing, deterministic time (testing/synctest), goroutine leak detection (goleak), HTTP handlers. Apply proactively when a function gets a new test or a test is flaky. Benchmark methodology: see go-benchmark.
5 other projects use a version of this.
Adopted by
Most recently taken up by
- AllThingsSmitty/proven-skillstheir own version, first seen Oct 6, 2026
- Integralist/agent-skillstheir own version, first seen Oct 4, 2026
- Dankosik/golang-backend-skillstheir own version, first seen Sep 8, 2026
- cxuu/golang-skillstheir own version, first seen Jun 20, 2026
- saisudhir14/claude-skillstheir own version, first seen Mar 25, 2026
Other versions5 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 5
- cxuu/golang-skills★ 165same name
- saisudhir14/claude-skills★ 9same name
- Integralist/agent-skills★ 3same name
- AllThingsSmitty/proven-skills★ 1same name
- Dankosik/golang-backend-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository26 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- go-clean-architectureSkill
- go-code-reviewSkill
- go-code-styleSkill
- go-concurrencySkill
- go-contextSkill
- go-control-flowSkill
- go-databaseSkill
- go-data-structuresSkill
- go-declarationsSkill
- go-defensiveSkill
- go-documentationSkill
- go-error-handlingSkill
- go-functional-optionsSkill
- go-functionsSkill
- go-genericsSkill
- go-graphqlSkill
- go-grpcSkill
- go-interfacesSkill
- go-lintingSkill
- go-loggingSkill
- go-namingSkill
- go-observabilitySkill
- go-packagesSkill
- go-performanceSkill
- go-swaggerSkill
The file
We split this skill into the rules it gives an agent
10 rules. 0 are common, 1 is distinctive, and 9 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
9
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
Failures must be diagnosable from the log alone: Every t.Errorf includes the function under test, the inputs, what we got, and what we …
- Very specific: line 16
No assertion libraries by default: Use the standard t.Errorf / t.Fatalf plus go-cmp for structural comparison.
- Distinctive: line 17
Test observable behaviour, not implementation details: If a refactor that preserves behaviour breaks the test, the test was wrong.
- Very specific: line 18
Each test runs independently: No execution-order dependencies, no shared global state without t.Cleanup.
- Very specific: line 38
Always print got before want.
- Very specific: line 52
Use t.Error by default; reserve t.Fatal for "the next line is meaningless without this value" (failed setup, failed decode before use).
- Very specific: line 83
Every case has a name used in t.Run.
- Very specific: line 101
Use httptest with table-driven cases.
- Very specific: line 131
Use synctest.Test on Go 1.25+ and 1.26+.
- Very specific: line 151
Run with go test -tags=integration ./....
“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Go Testing
Tests are executable specifications. Their job is to fail usefully when behaviour regresses — and to keep failing in the same way until the bug is fixed. Tests that are passing-or-flaky teach the team to ignore them, which is worse than no test at all.
Core Rules
- Failures must be diagnosable from the log alone. Every
t.Errorfincludes the function under test, the inputs, what we got, and what we wanted, in that order. - No assertion libraries by default. Use the standard
t.Errorf/t.Fatalfplusgo-cmpfor structural comparison.testifyis acceptable when adopted consistently — pick one and stick with it. - Test observable behaviour, not implementation details. If a refactor that preserves behaviour breaks the test, the test was wrong.
- Each test runs independently. No execution-order dependencies, no shared global state without
t.Cleanup. t.Parallel()whenever the test is safe to run in parallel. Most are.t.Helper()is the first line of any helper function that callst.Errorf/t.Fatalf. Reservet.Fatalfor "next line is meaningless without this value"; everything else usest.Error. Never callt.Fatal/t.FailNowfrom a non-test goroutine — send the failure back via channel.
"Useful Failures" Format
The failure message is the test's user interface. The canonical shape:
FunctionUnderTest(input) = got, want want// Good
t.Errorf("Add(2, 3) = %d, want %d", got, 5)
// Bad — no function, no inputs, reversed
t.Errorf("expected %d but got %d", 5, got)Always print got before want. With cmp.Diff(want, got), the diff shows (-want +got) — echo that direction in your message.
"No Asserts" Philosophy
Standard-library testing with if + t.Errorf reads as plain Go and produces messages you control. Assertion libraries shorten call sites but trade away message quality and reorder the got/want convention.
- Default —
if got != want { t.Errorf("...") }pluscmp.Difffor structs, slices, maps, protos. - Permitted —
testifyif the team agrees andtestifylintis enabled.requireonly when continuing is meaningless. - Avoid — mixing styles within the same package.
For protocol buffers, add protocmp.Transform() as a cmp option. Don't diff serialised JSON strings — decode and cmp.Diff instead.
t.Error vs t.Fatal
Use t.Error by default; reserve t.Fatal for "the next line is meaningless without this value" (failed setup, failed decode before use). Never call t.Fatal/t.FailNow from a goroutine other than the test goroutine — it does not stop the test. Send the failure back via channel.
Read references/assertions-and-helpers.md when designing helpers, custom comparers, or migrating between stdlib testing and
testify.
Table-Driven Tests
func TestCalculatePrice(t *testing.T) {
tests := []struct {
name string
quantity int
unitPrice float64
want float64
}{
{"single item", 1, 10.0, 10.0},
{"bulk discount", 100, 10.0, 900.0},
{"zero quantity", 0, 10.0, 0.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := CalculatePrice(tt.quantity, tt.unitPrice)
if got != tt.want {
t.Errorf("CalculatePrice(%d, %.2f) = %.2f, want %.2f",
tt.quantity, tt.unitPrice, got, tt.want)
}
})
}
}Every case has a name used in t.Run; failure messages include inputs, not the row index. When cases need different mocks or assertion shapes, stop using a table and write separate functions.
Helpers, Cleanup, Parallel
func setupTestDB(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
if err != nil { t.Fatalf("open db: %v", err) }
t.Cleanup(func() { _ = db.Close() })
return db
}t.Helper() is the first line of any helper that may fail; t.Cleanup runs after the test (and subtests) in LIFO order. Call t.Parallel() inside the subtest function. The paralleltest linter catches missing calls and the pre-1.22 loop-variable trap.
HTTP Handlers
Use httptest with table-driven cases. See references/http-and-fakes.md for request/response body, header, and status assertions.
Goroutine Leaks: goleak
Wire go.uber.org/goleak into every package that spawns goroutines:
import "go.uber.org/goleak"
func TestMain(m *testing.M) { goleak.VerifyTestMain(m) }Per-test: defer goleak.VerifyNone(t). Exclusions go to goleak.IgnoreTopFunction(...) — avoid IgnoreAnyFunction.
Deterministic Time: testing/synctest
For timer/context/deadline tests, testing/synctest (Go 1.25+) gives reproducible ordering. Synthetic time advances only when every goroutine in the bubble is blocked:
synctest.Test(t, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
time.Sleep(5 * time.Second)
synctest.Wait()
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatalf("got %v, want DeadlineExceeded", ctx.Err())
}
})Use synctest.Test on Go 1.25+ and 1.26+. The Go 1.24 GOEXPERIMENT=synctest synctest.Run API is only for modules still on 1.24.
Fuzzing and Benchmarks
Native fuzzing finds inputs you would never write by hand. Seed with f.Add, then f.Fuzz; check fuzzer-discovered corpora (testdata/fuzz/...) into git as regression tests.
Benchmarks use for b.Loop() on Go 1.24+ (the legacy b.N loop only when targeting older versions). Sub-benchmarks across sizes use b.Run(fmt.Sprintf("n=%d", n), ...). For methodology, benchstat, and CI regression detection, see a dedicated go-benchmark skill — not this one.
Read references/fuzz-synctest-bench.md when wiring fuzz corpora into CI, designing
synctest-based timer tests, or writing comparable benchmark suites withb.Loop.
Integration Tests
Separate by build tag so go test ./... stays fast:
//go:build integration
package mypackage_testRun with go test -tags=integration ./.... Integration tests own their fixtures (containers, schemas, fixtures) via t.Cleanup.
Read references/http-and-fakes.md when writing HTTP handler tests, mocking via consumer-owned interfaces, or stubbing time.
Anti-Patterns
| Anti-pattern | Do this instead |
t.Errorf("got %d", got) without the function or wanted value | FunctionUnderTest(input) = got, want want |
Comparing error strings (err.Error() == "...") | errors.Is / errors.As |
Calling t.Fatal from a spawned goroutine | Send via channel; main goroutine calls t.Fatal |
| Tables of cases with conditional setup per row | Split into separate test functions |
Subtests without t.Run(tt.name, ...) | Always name and t.Run |
| Mocking concrete types from another package | Define a small interface in the consumer; pass a fake |
time.Sleep to wait for "the goroutine to do its thing" | synctest.Test or explicit synchronisation |
| Snapshot tests that compare serialised JSON | Decode then cmp.Diff |
Mutating os.Args/env/flag.CommandLine without t.Cleanup | Save and restore in t.Cleanup |
Verification Checklist
- Every failure message includes function, inputs, got, and want, in that order
cmp.Diffcalls use(-want +got)order and echo it in the message- Table-driven cases have
namefields and uset.Run - Helpers call
t.Helper()and uset.Cleanupfor teardown - Parallel-safe tests call
t.Parallel();paralleltestis clean - Packages that spawn goroutines wire
goleak.VerifyTestMainor per-testVerifyNone - Timer/deadline tests use
testing/synctest, nottime.Sleep - Integration tests are gated by a build tag and own their fixtures
go test -race ./...is clean; fuzz corpora are checked intotestdata/fuzz/...
References
- references/assertions-and-helpers.md — useful failures,
cmp.Diff, helpers,testifyinterop - references/http-and-fakes.md —
httptest, consumer-owned fakes, time stubs - references/fuzz-synctest-bench.md — fuzzing,
testing/synctest,b.Loopbenchmarks - references/goleak-and-flakes.md — leak detection, flake diagnosis, race triage
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

