返回顶部
g

go-testing-code-reviewGo测试代码审查

Reviews Go test code for proper table-driven tests, assertions, and coverage patterns. Use when reviewing *_test.go files.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 2.3.0
安全检测
已通过
132
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

go-testing-code-review

Go 测试代码审查

快速参考

问题类型参考文档
测试结构、命名references/structure.md
模拟、接口
references/mocking.md |

审查清单

  • - [ ] 测试采用表格驱动方式,包含清晰的用例名称
  • [ ] 子测试使用 t.Run 实现并行执行
  • [ ] 测试名称描述行为,而非实现细节
  • [ ] 错误信息包含 got/want 及描述性消息
  • [ ] 清理操作通过 t.Cleanup 注册
  • [ ] 并行测试不共享可变状态
  • [ ] 模拟使用测试文件中定义的接口
  • [ ] 覆盖率包含边界情况和错误路径
  • [ ] 性能关键函数包含 Benchmark 测试
  • [ ] 输入解析器/验证器包含 Fuzz 测试(Go 1.18+)
  • [ ] HTTP 处理器使用 httptest.NewRequest/httptest.NewRecorder 测试
  • [ ] 黄金文件测试使用 testdata/*.golden 模式及 -update 标志

关键模式

表格驱动测试

go
// 错误 - 重复代码
func TestAdd(t *testing.T) {
if Add(1, 2) != 3 {
t.Error(错误)
}
if Add(0, 0) != 0 {
t.Error(错误)
}
}

// 正确
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
want int
}{
{正数, 1, 2, 3},
{零值, 0, 0, 0},
{负数, -1, 1, 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf(Add(%d, %d) = %d, 期望 %d, tt.a, tt.b, got, tt.want)
}
})
}
}

错误消息

go
// 错误
if got != want {
t.Error(结果错误)
}

// 正确
if got != want {
t.Errorf(GetUser(%d) = %v, 期望 %v, id, got, want)
}

// 复杂类型
if diff := cmp.Diff(want, got); diff != {
t.Errorf(GetUser() 不匹配 (-期望 +实际):\n%s, diff)
}

并行测试

go
func TestFoo(t *testing.T) {
tests := []struct{...}

for _, tt := range tests {
tt := tt // 捕获变量(Go 1.22+ 不需要)
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// 测试代码
})
}
}

清理操作

go
// 错误 - 手动清理,失败时跳过
func TestWithTempFile(t *testing.T) {
f, _ := os.CreateTemp(, test)
defer os.Remove(f.Name()) // 测试 panic 时跳过
}

// 正确
func TestWithTempFile(t *testing.T) {
f, _ := os.CreateTemp(, test)
t.Cleanup(func() {
os.Remove(f.Name())
})
}

其他模式

基准测试

go func BenchmarkProcess(b *testing.B) { data := generateTestData(1000) b.ResetTimer()

for i := 0; i < b.N; i++ {
Process(data)
}
}

// 运行:go test -bench=BenchmarkProcess -benchmem

模糊测试(Go 1.18+)

go func FuzzParseInput(f *testing.F) { // 种子语料库 f.Add({name: test}) f.Add() f.Add({invalid})

f.Fuzz(func(t *testing.T, input string) {
result, err := ParseInput(input)
if err != nil {
return // 无效输入是预期的
}
// 如果解析成功,重新编码应正常工作
if _, err := json.Marshal(result); err != nil {
t.Errorf(解析后 Marshal: %v, err)
}
})
}

// 运行:go test -fuzz=FuzzParseInput -fuzztime=30s

HTTP 处理器测试

go func TestHandler(t *testing.T) { srv := NewServer(mockDeps)

req := httptest.NewRequest(GET, /api/users/123, nil)
w := httptest.NewRecorder()

srv.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Errorf(状态码 = %d, 期望 %d, w.Code, http.StatusOK)
}
}

黄金文件

go var update = flag.Bool(update, false, 更新黄金文件)

func TestRender(t *testing.T) {
got := Render(input)
golden := filepath.Join(testdata, t.Name()+.golden)

if *update {
if err := os.WriteFile(golden, got, 0644); err != nil {
t.Fatalf(写入黄金文件: %v, err)
}
}

want, err := os.ReadFile(golden)
if err != nil {
t.Fatalf(读取黄金文件: %v(使用 -update 创建), err)
}
if !bytes.Equal(got, want) {
t.Errorf(输出不匹配:\n实际:\n%s\n期望:\n%s, got, want)
}
}

反模式

1. 测试内部实现

go
// 错误 - 测试私有状态
func TestUser(t *testing.T) {
u := NewUser(alice)
if u.id != 1 { // 测试内部字段
t.Error(ID 错误)
}
}

// 正确 - 测试行为
func TestUser(t *testing.T) {
u := NewUser(alice)
if u.ID() != 1 {
t.Error(ID 错误)
}
}

2. 共享可变状态

go
// 错误 - 测试相互干扰
var testDB = setupDB()

func TestA(t *testing.T) {
t.Parallel()
testDB.Insert(...) // 竞态条件!
}

// 正确 - 每个测试隔离
func TestA(t *testing.T) {
db := setupTestDB(t)
t.Cleanup(func() { db.Close() })
db.Insert(...)
}

3. 无上下文的断言

go
// 错误
assert.Equal(t, want, got) // 期望 X 得到 Y - 哪个测试?

// 正确
assert.Equal(t, want, got, 更新后的用户名)

何时加载参考文档

  • - 审查测试文件结构 → structure.md
  • 审查模拟实现 → mocking.md

审查问题

  1. 1. 测试是否采用表格驱动方式并包含命名用例?
  2. 错误消息是否包含输入、实际值和期望值?
  3. 并行测试是否隔离(无共享状态)?
  4. 清理操作是否通过 t.Cleanup 完成?
  5. 测试是否验证行为,而非实现细节?

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 go-testing-code-review-1776089642 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 go-testing-code-review-1776089642 技能

通过命令行安装

skillhub install go-testing-code-review-1776089642

下载

⬇ 下载 go-testing-code-review v2.3.0(免费)

文件大小: 7.91 KB | 发布时间: 2026-4-14 13:22

v2.3.0 最新 2026-4-14 13:22
- Added comprehensive review checklist for Go test code, covering table-driven, parallel, and fuzz tests.
- Expanded documentation with anti-patterns, critical testing patterns, and example code snippets.
- Included references for structure and mocking best practices.
- Clarified best practices for error messages, cleanup, and isolation in tests.
- Added guidance for benchmarks, fuzz tests, HTTP handlers, and golden files.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部