Go testing best practices including table-driven tests, race detection, test coverage, and mocking strategies. Use when writing or reviewing Go tests.
Provides Go testing expertise for table-driven tests, race detection, coverage, and mocking. Use when writing or reviewing Go tests to ensure best practices.
/plugin marketplace add jovermier/cc-stack-marketplace/plugin install cc-go@cc-stack-marketplaceThis skill inherits all available tools. When active, it can use any tool Claude has access to.
references/concurrency.mdreferences/coverage.mdreferences/mocking.mdreferences/table-driven.mdExpert guidance for writing maintainable, effective Go tests.
| Pattern | When to Use | Structure |
|---|---|---|
| Table-driven tests | Multiple inputs/outputs | []struct with test cases |
| Subtests | Related test variants | t.Run() for each case |
| TestMain | Global setup/teardown | func TestMain(m *testing.M) |
| t.Cleanup | Per-test cleanup | Deferred cleanup function |
| fakes/fuzzing | Random input testing | testing.F, f.Fuzz() |
| Race detector | Concurrent code | go test -race |
| Coverage | Ensuring thoroughness | go test -cover |
Specify a number or describe your testing scenario.
| Response | Reference to Read |
|---|---|
| 1, "table", "driven", "multiple cases" | table-driven.md |
| 2, "mock", "fake", "interface" | mocking.md |
| 3, "race", "concurrent", "parallel" | concurrency.md |
| 4, "coverage", "measure", "thorough" | coverage.md |
| 5, general testing | Read relevant references |
func TestFunctionName(t *testing.T) {
tests := []struct {
name string
input InputType
want WantType
wantErr bool
errIs error
}{
{
name: "successful case",
input: InputType{...},
want: WantType{...},
wantErr: false,
},
{
name: "validation error",
input: InputType{...},
wantErr: true,
errIs: ErrValidation,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FunctionName(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("FunctionName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.errIs != nil && !errors.Is(err, tt.errIs) {
t.Errorf("FunctionName() error = %v, wantIs %v", err, tt.errIs)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FunctionName() = %v, want %v", got, tt.want)
}
})
}
}
project/
├── internal/
│ ├── service/
│ │ ├── service.go
│ │ ├── service_test.go
│ │ └── service_golden_test.go
│ └── service/
│ ├── mocks/ # Generated mocks (if needed)
│ └── testdata/ # Golden files, fixtures
└── testutil/
├── setup.go # Test helpers
└── fixtures.go # Shared test data
func TestHandler(t *testing.T) {
tests := []struct {
name string
method string
body string
wantStatus int
wantBody string
}{
{"valid POST", "POST", `{"foo":"bar"}`, 200, `{"result":"ok"}`},
{"invalid JSON", "POST", `{`, 400, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(tt.method, "/test", strings.NewReader(tt.body))
rec := httptest.NewRecorder()
Handler(rec, req)
if rec.Code != tt.wantStatus {
t.Errorf("status = %d, want %d", rec.Code, tt.wantStatus)
}
})
}
}
func TestWithCleanup(t *testing.T) {
// Setup
db := openTestDB(t)
t.Cleanup(func() {
db.Close() // Runs even if test fails
})
// Test code...
}
# Run tests with race detector
go test -race ./...
# Run specific test with race detector
go test -race -run TestConcurrentFunction
| File | Topics |
|---|---|
| table-driven.md | Table structure, subtests, naming |
| mocking.md | Interfaces, fakes, mocking libraries |
| concurrency.md | Race detector, parallel tests, sync |
| coverage.md | -cover, -coverprofile, thresholds |
Tests are good when:
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.