Agent skill
golang-testing
Go testing patterns and policies. Use when writing Go test code, following TDD, or reviewing Go test code. Do NOT use for application code -- use golang-patterns instead.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/golang-testing-cooldaemon-dotfiles
SKILL.md
Go Testing Patterns
Table-Driven Tests (REQUIRED)
Use table-driven tests for all Go tests:
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
expected int
}{
{"positive", 2, 3, 5},
{"negative", -1, -2, -3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Add(tt.a, tt.b); got != tt.expected {
t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.expected)
}
})
}
}
With Error Cases
tests := []struct {
name string
input string
want *Config
wantErr bool
}{
{"valid", `{"host":"localhost"}`, &Config{Host: "localhost"}, false},
{"invalid", `{bad}`, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Parse(tt.input)
if tt.wantErr {
if err == nil {
t.Error("expected error")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// assert got == tt.want
})
}
Test Helper Policies
Always Use t.Helper()
func assertNoError(t *testing.T, err error) {
t.Helper() // REQUIRED - improves error location reporting
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
Always Use t.Cleanup()
func setupTestDB(t *testing.T) *sql.DB {
t.Helper()
db, _ := sql.Open("sqlite3", ":memory:")
t.Cleanup(func() { db.Close() }) // REQUIRED - auto cleanup
return db
}
Use t.TempDir() for Files
tmpDir := t.TempDir() // Auto-cleaned after test
testFile := filepath.Join(tmpDir, "test.txt")
Parallel Tests
for _, tt := range tests {
tt := tt // Capture loop variable (required before Go 1.22)
t.Run(tt.name, func(t *testing.T) {
t.Parallel() // Run in parallel when tests are independent
// ...
})
}
Interface-Based Mocking
Define interfaces where used, create mock implementations:
// In consumer package
type UserStore interface {
GetUser(id string) (*User, error)
}
// Mock for tests
type MockUserStore struct {
GetUserFunc func(id string) (*User, error)
}
func (m *MockUserStore) GetUser(id string) (*User, error) {
return m.GetUserFunc(id)
}
Coverage Targets
| Code Type | Target |
|---|---|
| Critical business logic | 100% |
| Public APIs | 90%+ |
| General code | 80%+ |
| Generated code | Exclude |
Commands
See makefile-first skill for command execution policy.
Anti-Patterns
| Anti-Pattern | Correct Approach |
|---|---|
| Testing private functions | Test through public API |
time.Sleep() in tests |
Use channels or sync primitives |
| Ignoring flaky tests | Fix or remove immediately |
| Skip error path testing | Always test error cases |
Missing t.Helper() |
Always add to helper functions |
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?