
Go Test
- 4 installs
- 22 repo stars
- Updated December 29, 2025
- tencent/awesome-devbuddy
Helps with testing & qa tasks during AI-assisted development.
About
go-test is a Claude Code skill for testing & qa. It helps solo builders move faster with AI-assisted coding.
- go-test
- Testing & QA
- AI-coding skill
Go Test by the numbers
- 4 all-time installs (skills.sh)
- Ranked #1,631 of 2,153 Testing & QA skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/tencent/awesome-devbuddy --skill go-testAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 4 |
|---|---|
| repo stars | ★ 22 |
| Last updated | December 29, 2025 |
| Repository | tencent/awesome-devbuddy ↗ |
What it does
Helps with testing & qa tasks during AI-assisted development.
Files
Go 单测技能
你是一个 Go 测试专家,帮助用户生成、运行和分析 Go 项目的单元测试。
核心能力
1. 生成测试:使用 testify/assert 和 gomonkey 创建单元测试模板 2. 运行测试:自动检测 ARM64 架构并执行测试 3. 覆盖率报告:生成并分析测试覆盖率 4. CI/CD 集成:提供适合流水线的覆盖率报告格式
执行步骤
步骤1:理解用户意图
识别用户需求:
- 生成测试:关键词 "生成"/"generate"/"创建测试"
- 运行测试:关键词 "运行"/"run"/"执行测试"
- 覆盖率:关键词 "覆盖率"/"coverage"
- 完整流程:关键词 "全流程"/"all"/"完整"
如果不明确,使用 AskUserQuestion 工具询问。
步骤2:执行任务
生成测试代码
针对指定的 Go 文件或函数: 1. 读取源文件 2. 分析函数签名和依赖 3. 生成测试文件,包含:
- Table-driven 测试结构
- testify/assert 断言
- gomonkey mock 外部依赖
- 成功和失败测试用例
示例模板:
func TestFuncName(t *testing.T) {
tests := []struct {
name string
args Args
want Result
wantErr bool
}{
{"成功场景", Args{}, Result{}, false},
{"失败场景", Args{}, Result{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
patches := gomonkey.ApplyFunc(ExternalFunc, func() error {
return nil
})
defer patches.Reset()
got, err := FuncName(tt.args)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}运行测试
1. 检测系统架构:uname -m 2. 构建测试命令:
- ARM64/aarch64:添加
-gcflags="all=-l"(gomonkey 必需) - 所有架构:包含
-v -cover -race
3. 根据范围执行:
- 全量:
go test ./... - 指定包:
go test ./path/to/package/... - 单文件:
go test ./path/to/file_test.go - 单函数:
go test -run TestName ./package/
4. 解析并报告结果
生成覆盖率报告
# 生成覆盖率数据
go test -coverprofile=coverage.out -covermode=atomic ./...
# 生成 HTML 报告
go tool cover -html=coverage.out -o coverage.html
# 显示总体覆盖率
go tool cover -func=coverage.out | grep total
# 检查阈值(默认 70%)
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
if (( $(echo "$COVERAGE < 70" | bc -l) )); then
echo "❌ 覆盖率 $COVERAGE% 低于阈值"
else
echo "✅ 覆盖率 $COVERAGE% 达标"
fi步骤3:报告结果
提供清晰的总结:
- ✅ 通过的测试数量
- ❌ 失败的测试及错误信息
- 📊 覆盖率统计
- ⏱️ 执行时间
- 🔗 HTML 报告链接(如果生成)
项目配置
针对 data-access-server 项目:
- 默认测试包:
./services/ustask/...,./services/ckmove/...,./client/... - 覆盖率阈值:70%
- 必需框架:testify + gomonkey
- ARM64 特殊参数:
-gcflags="all=-l"(强制)
错误处理
常见错误的解决方案:
- "provider not exist":检查 tRPC 配置文件
- gomonkey panic:添加
-gcflags="all=-l"参数 - race detected:提供竞态报告和修复建议
- timeout:建议使用
-timeout参数或优化测试
最佳实践提醒
主动提醒用户: 1. 检测到 ARM64 架构 → 已自动添加 -gcflags="all=-l" 2. 覆盖率低 → 建议需要测试的关键函数 3. 竞态条件 → 提供修复建议 4. 慢测试(>1秒)→ 建议优化 5. Mock 清理 → 确保使用 defer patches.Reset()
根据用户输入执行任务并提供简洁、可操作的结果。
Go 单测管理 Skill
一个集成的 Go 单元测试管理工具,帮助快速生成测试、运行测试并生成覆盖率报告
📚 功能概览
- ✅ 智能生成测试:自动生成 Table-Driven 风格的单元测试代码
- 🚀 一键运行测试:自动适配 ARM64 架构,无需手动配置参数
- 📊 覆盖率分析:生成 HTML 可视化报告和覆盖率统计
- 🔍 CI/CD 友好:支持流水线集成和阈值检查
🚀 快速开始
调用 Skill
在 Terminal Assistant Agent 中输入:
/go-test然后根据交互提示选择操作,或者直接描述需求:
帮我生成 services/ustask/modify_config.go 的单元测试
运行 services/ustask 包的所有测试
生成项目的覆盖率报告
执行完整的测试流程💡 使用场景
场景 1:为新功能生成测试
需求:刚完成 services/ustask/new_feature.go,需要补充单元测试
操作:
请帮我为 services/ustask/new_feature.go 生成单元测试Skill 会自动:
- 分析函数签名和依赖关系
- 生成
new_feature_test.go文件 - 包含 testify 断言和 gomonkey mock
- 提供成功和失败场景的测试用例
场景 2:运行测试并检查覆盖率
需求:提交代码前确保测试通过且覆盖率达标
操作:
运行全量测试并生成覆盖率报告输出示例:
🧪 执行配置:
• 测试范围: ./...
• 架构: arm64 (已自动添加 -gcflags="all=-l")
• 选项: -v -cover -race
✅ 测试结果:
• 通过: 45/48
• 失败: 3/48
• 耗时: 12.3s
📊 覆盖率统计:
• 总体覆盖率: 72.5% ✅ (阈值: 70%)
• services/ustask: 75.3%
• services/ckmove: 71.2%
📁 报告文件:
• coverage.html 👉 file:///.../coverage.html场景 3:调试单个失败的测试
需求:CI 流水线显示 TestModifyConfig 失败,需要本地调试
操作:
运行 TestModifyConfig 测试函数Skill 会:
- 自动检测架构并添加必要参数
- 以详细模式运行指定测试
- 显示完整的错误堆栈信息
场景 4:生成增量覆盖率
需求:只关心本次 MR 新增代码的测试覆盖率
操作:
生成增量覆盖率报告Skill 会:
- 对比 main 分支找出变更文件
- 运行完整测试生成覆盖率数据
- 筛选变更文件的覆盖率
- 计算增量覆盖率百分比
🛠️ 使用 Makefile 命令
项目已集成相关 make 命令,可直接使用:
# 生成覆盖率报告
make test-coverage
# 覆盖率阈值检查(适合 CI)
make test-coverage-check
# 运行指定包测试
make test-pkg PKG=./services/ustask/...
# 清理测试产物
make clean-test🔧 Skill 支持的操作
| 操作类型 | 触发关键词 | 说明 |
|---|---|---|
| 生成测试 | 生成/generate/创建测试 | 为指定文件生成测试模板 |
| 运行测试 | 运行/run/执行测试 | 执行单元测试 |
| 覆盖率报告 | 覆盖率/coverage | 生成覆盖率数据和 HTML 报告 |
| 完整流程 | 全流程/all/完整 | 运行测试 + 覆盖率 + 阈值检查 |
🎯 生成的测试代码规范
Skill 生成的测试代码遵循以下规范:
package yourpackage
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/agiledragon/gomonkey/v2"
)
func TestYourFunction(t *testing.T) {
tests := []struct {
name string
args YourArgs
want YourResult
wantErr bool
}{
{
name: "成功场景",
args: YourArgs{/* ... */},
want: YourResult{/* ... */},
wantErr: false,
},
{
name: "失败场景",
args: YourArgs{/* ... */},
want: YourResult{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mock 外部依赖
patches := gomonkey.ApplyFunc(ExternalFunc, func() error {
return nil
})
defer patches.Reset()
// 执行测试
got, err := YourFunction(tt.args)
// 断言
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}特点:
- Table-Driven 测试结构
- testify/assert 断言库
- gomonkey mock 框架
- 清晰的成功/失败场景划分
🏗️ CI/CD 集成
GitLab CI 配置示例
test:
stage: test
script:
- make test-coverage-check
coverage: '/total:\s+\(statements\)\s+(\d+\.\d+)%/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.out
paths:
- coverage.html
expire_in: 30 days
only:
- merge_requests
- main工蜂流水线配置
test_job:
steps:
- name: 运行单元测试
script: |
make test-coverage-check
- name: 上传覆盖率报告
script: |
curl -F "file=@coverage.html" https://your-artifact-server/upload⚙️ 项目配置
当前项目(data-access-server)的默认配置:
- 默认测试包:
./services/ustask/...,./services/ckmove/...,./client/... - 覆盖率阈值:70%
- 必需框架:testify + gomonkey
- ARM64 特殊处理:自动添加
-gcflags="all=-l"参数
🐛 常见问题
Q1: ARM64 架构报错 "gomonkey: please add -gcflags=all=-l"
原因:gomonkey 在 ARM64 架构下需要禁用内联优化
解决方案:
- ✅ 使用 Skill 或 make 命令会自动处理
- ⚠️ 手动运行需添加:
go test -gcflags="all=-l" ./...
Q2: 测试报错 "trpc/config: provider not exist"
原因:测试依赖 tRPC 配置环境
解决方案:
- 检查
trpc_go_local.yaml配置是否正确 - 或在测试中 mock 配置依赖
Q3: 覆盖率包含了 vendor 代码怎么办?
原因:默认 ./... 会包含所有子目录
解决方案:
- 使用
-coverpkg指定范围:go test -coverpkg=./services/...,./client/... ./... - 或修改 Makefile 排除 vendor
Q4: 如何跳过慢测试?
使用 Skill:
运行测试,跳过慢测试手动运行:
go test -short ./...📊 覆盖率报告说明
HTML 报告
打开 coverage.html 查看可视化覆盖率:
- 🟢 绿色:已覆盖代码
- 🔴 红色:未覆盖代码
- 🟡 黄色:部分覆盖(如 if-else 只覆盖一个分支)
点击文件名可查看详细的行级覆盖情况。
命令行统计
# 查看总体覆盖率
go tool cover -func=coverage.out | grep total
# 查看各包覆盖率
go tool cover -func=coverage.out