一键导入
go-testing
Go testing best practices including table-driven tests, race detection, test coverage, and mocking strategies. Use when writing or reviewing Go tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Go testing best practices including table-driven tests, race detection, test coverage, and mocking strategies. Use when writing or reviewing Go tests.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | go-testing |
| description | Go testing best practices including table-driven tests, race detection, test coverage, and mocking strategies. Use when writing or reviewing Go tests. |
Expert 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:
Automatically discover and install relevant skills from SkillsMP and other sources
Go error handling patterns including wrapping, custom error types, errors.Is/As, and error conventions. Use when handling, creating, or checking errors in Go.
Go performance optimizations including memory allocation reduction, efficient string building, I/O operations, and resource pooling. Use when optimizing Go code for speed or memory efficiency.
GraphQL resolver patterns including dataloader for N+1 prevention, context propagation, authorization, error handling, and validation. Use when implementing GraphQL resolvers.
GraphQL schema design including types, fields, pagination, nullability, naming conventions, and descriptions. Use when designing or modifying GraphQL schemas.
Next.js Metadata API for SEO, OpenGraph tags, structured data, and social sharing. Use when implementing metadata, SEO, or social media previews.