← ClaudeAtlas

go-testinglisted

Go testing patterns and conventions — table-driven tests, test helpers, mocks, and golden files.
NeerajG03/JEFF · ★ 1 · Testing & QA · score 72
Install: claude install-skill NeerajG03/JEFF
# Go Testing Standardised approach to writing Go tests in this codebase. Tests should be readable, deterministic, and fast. ## Table-Driven Tests Use sub-tests with a named struct slice: ```go func TestParseDuration(t *testing.T) { tests := []struct { name string input string want time.Duration err bool }{ {name: "seconds", input: "30s", want: 30 * time.Second}, {name: "invalid", input: "xyz", err: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := parseDuration(tt.input) if (err != nil) != tt.err { t.Fatalf("parseDuration(%q) err = %v, want err=%v", tt.input, err, tt.err) } if got != tt.want { t.Errorf("parseDuration(%q) = %v, want %v", tt.input, got, tt.want) } }) } } ``` ## Test Helpers Place shared helpers in `_test.go` files in a `testutil` package or in `export_test.go` for white-box testing of unexported symbols: ```go // export_test.go — exports internal symbols for testing. package mypackage var ParseConfig = parseConfig ``` Helper functions should call `t.Helper()`: ```go func tempDir(t *testing.T) string { t.Helper() dir, err := os.MkdirTemp("", "test-*") if err != nil { t.Fatal(err) } t.Cleanup(func() { os.RemoveAll(dir) }) return dir } ``` ## Mocking Prefer hand-written mocks over mock generators. Keep mocks mi