关于
This Claude Skill provides expert guidance on implementing and refactoring table-driven tests in Go. It covers best practices for struct design, subtest naming, advanced patterns like test matrices, and deciding when to use or avoid this approach. Use it specifically for writing, reviewing, or cleaning up table-driven tests, not for general testing strategies or other test types.
快速安装
Claude Code
推荐npx skills add eduardo-sl/go-agent-skills -a claude-code/plugin add https://github.com/eduardo-sl/go-agent-skillsgit clone https://github.com/eduardo-sl/go-agent-skills.git ~/.claude/skills/go-test-table-driven在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Go Table-Driven Tests
Table-driven tests are a powerful Go idiom — when used correctly. Most codebases either underuse them (10 copy-paste tests) or overuse them (complex branching logic in a 200-line struct). This skill covers the sweet spot.
Detailed reference material, loaded on demand:
references/patterns.md— full worked examples: canonical tables,wantErr/wantErrIs, parallel tables, map-based tables, error-only tables, struct alignment for readability.references/refactoring.md— recognizing bloated tables and rewriting them as explicit subtests, with before/after examples.
Read a reference file only when the summary below is not enough for the task at hand.
1. When Table-Driven Tests Shine
Use a table only when ALL of these are true:
- Same function under test across all cases
- Same assertion pattern — input in, output out, compare
- Cases differ only in data, not in setup or verification logic
- 3+ cases — fewer than 3, explicit tests are clearer
Canonical use case: pure functions, parsers, validators, formatters.
func TestParseSize(t *testing.T) {
tests := []struct {
name string
input string
want int64
wantErr bool
}{
{name: "plain bytes", input: "1024", want: 1024},
{name: "kilobytes suffix", input: "4KB", want: 4096},
{name: "empty string", input: "", wantErr: true},
{name: "negative size", input: "-1", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseSize(tt.input)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
Every case has the same shape, the loop body is a few lines, and adding a case is one struct literal. No branching, no conditionals.
2. When NOT to Use Table-Driven Tests
- Complex per-case setup —
setupMock/setupFuncfunction fields in the struct mean the table is hiding complexity. Write explicit subtests. - Fewer than 3 cases — the struct definition is more code than two plain test functions.
- Multiple branching paths —
if tt.shouldError/if tt.wantRedirectin the loop body means each branch is a different test pretending to share a structure. Split it.
See references/refactoring.md for before/after rewrites of each smell.
3. Struct Design Rules
- Every field must vary between at least 2 cases. A field with the same value everywhere is setup — move it outside the table.
- Name the
namefield as a short sentence describing the scenario:"returns error for negative amount", not"case1"or"success". wantErr boolfor "should it error?" — check it first andreturnearly in the loop body.wantErrIs errorwith a sentinel when the caller must detect a specific error; assert withrequire.ErrorIs.- ≤5 fields. More means the scenario is too complex for a table — split into separate test functions.
Full field-pattern examples are in references/patterns.md.
4. The Loop Body Must Be Trivial
The point of a table test is identical execution logic for every case. Keep the loop body under ~10 lines: call, error check, comparison. If it accumulates conditionals or per-case setup, the table has outgrown its usefulness — refactor into explicit subtests.
5. Parallel Table Tests
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := Transform(tt.input)
assert.Equal(t, tt.want, got)
})
}
- Go 1.22+ scopes the loop variable per iteration —
tt := ttcapture is unnecessary. For Go <1.22 the capture is still required. - Only use
t.Parallel()when the function under test has no side effects and no shared mutable state.
6. Refactoring Bloated Tables
| Symptom | Fix |
|---|---|
| Struct has 8+ fields | Split into multiple test functions by scenario |
setupFunc field in struct | Extract to separate subtests with explicit setup |
if tt.shouldX in loop body | Each branch is a different test — split it |
| Same 3 fields identical in every case | Move to shared setup outside the table |
| Adding a case requires understanding all others | Table has grown beyond its useful life |
Decision Flowchart
-
Is the function pure (input → output, no side effects)? Yes → table test is probably ideal. Go to 2. No → consider explicit subtests first.
-
Do all cases share the exact same assertion pattern? Yes → table test. Go to 3. No → explicit subtests.
-
Can each case be expressed in ≤5 struct fields? Yes → table test. No → split by scenario into separate test functions.
-
Is the loop body ≤10 lines? Yes → you're golden. No → the table is hiding complexity. Refactor.
Verification Checklist
- Table struct has only fields that vary between cases
- Every case has a descriptive
namefield - Loop body is ≤10 lines with no branching
- No
setupFuncormockFuncfields in the struct wantErris a simple bool or sentinel, not a string match- Cases cover: happy path, error path, edge cases (empty, nil, zero, max)
t.Runwraps each case for named subtestst.Parallel()used only when function is side-effect-free
GitHub 仓库
常见问题
什么是 go-test-table-driven Skill?
go-test-table-driven 是一个 Claude Skill,作者为 eduardo-sl。Skill 将 Claude 按需加载的说明和资源打包,让 Claude 无需额外提示即可执行与 go-test-table-driven 相关的任务。
如何安装 go-test-table-driven?
使用本页的安装命令:将 go-test-table-driven 作为插件添加到 Claude Code,或将其仓库克隆到 skills 目录,然后重启 Claude 以加载该 Skill。
go-test-table-driven 属于哪个分类?
go-test-table-driven 属于测试分类。
go-test-table-driven 可以免费使用吗?
可以。go-test-table-driven 已收录在 AIMCP,可免费安装。
相关推荐技能
该Skill通过60+个学术基准测试(如MMLU、GSM8K等)评估大语言模型质量,适用于模型对比、学术研究及训练进度追踪。它支持HuggingFace、vLLM和API接口,被EleutherAI等行业领先机构广泛采用。开发者可通过简单命令行快速对模型进行多任务批量评估。
这个Claude Skill提供了关于Cloudflare Cron Triggers的完整知识库,用于通过cron表达式定时执行Workers。它支持配置周期性任务、维护作业和自动化工作流,并能处理常见的cron触发错误。开发者可以用它来设置定时任务、测试cron处理器,并集成Workflows和Green Compute功能。
该Skill为开发者提供了基于Playwright的本地Web应用测试工具集,支持自动化测试前端功能、调试UI行为、捕获屏幕截图和查看浏览器日志。它包含管理服务器生命周期的辅助脚本,可直接作为黑盒工具运行而无需阅读源码。适用于需要快速验证本地Web应用界面和交互功能的开发场景。
这个Skill用于开发分支完成后的集成决策,当代码实现完成且测试通过时,它会引导开发者选择合适的工作流。它首先验证测试状态,然后提供合并、创建PR或清理等结构化选项。核心价值在于确保代码质量的同时,标准化分支收尾流程。
