go-test
关于
The go-test skill provides expertise in Go's standard testing package and best practices. It helps developers implement table-driven tests, subtests, benchmarks, and coverage strategies while following Go conventions. Use it when writing test files, creating mocks, detecting race conditions, or organizing integration tests in Go projects.
技能文档
Go Testing Code Guide
Test File Structure
One-to-one matching with the file under test. Test files should be located in the same directory as the target file.
File Naming
Format: {target-file-name}_test.go.
Example: user.go → user_test.go
Test Hierarchy
Organize by method (function) unit as major sections, and by test case as minor sections. Complex methods can have intermediate sections by scenario.
Test Coverage Selection
Omit obvious or overly simple logic (simple getters, constant returns). Prioritize testing business logic, conditional branches, and code with external dependencies.
Test Case Composition
At least one basic success case is required. Focus primarily on failure cases, boundary values, edge cases, and exception scenarios.
Test Independence
Each test should be executable independently. No test execution order dependencies. Initialize shared state for each test.
Given-When-Then Pattern
Structure test code in three stages—Given (setup), When (execution), Then (assertion). Separate stages with comments or blank lines for complex tests.
Test Data
Use hardcoded meaningful values. Avoid random data as it causes unreproducible failures. Fix seeds if necessary.
Mocking Principles
Mock external dependencies (API, DB, file system). For modules within the same project, prefer actual usage; mock only when complexity is high.
Test Reusability
Extract repeated mocking setups, fixtures, and helper functions into common utilities. Be careful not to harm test readability through excessive abstraction.
Integration/E2E Testing
Unit tests are the priority. Write integration/E2E tests when complex flows or multi-module interactions are difficult to understand from code alone. Place in separate directories (tests/integration, tests/e2e).
Test Naming
Test names should clearly express "what is being tested". Recommended format: "should do X when Y". Focus on behavior rather than implementation details.
Assertion Count
Multiple related assertions in one test are acceptable, but separate tests when validating different concepts.
Test Functions
Format: func TestXxx(t *testing.T). Write TestMethodName functions per method, compose subtests with t.Run().
Subtests
Pattern: t.Run("case name", func(t *testing.T) {...}). Each case should be independently executable. Call t.Parallel() when running in parallel.
Table-Driven Tests
Recommended when multiple cases have similar structure. Define cases with []struct{ name, input, want, wantErr }.
Example:
tests := []struct {
name string
input int
want int
wantErr bool
}{
{"normal case", 5, 10, false},
{"negative input", -1, 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Func(tt.input)
if (err != nil) != tt.wantErr { ... }
if got != tt.want { ... }
})
}
Mocking
Utilize interface-based dependency injection. Prefer manual mocking; consider gomock for complex cases. Define test-only implementations within _test.go.
Error Verification
Use errors.Is() and errors.As(). Avoid string comparison of error messages; verify with sentinel errors or error types instead.
Setup/Teardown
Use TestMain(m *testing.M) for global setup/teardown. For individual test preparation, do it within each test function or extract to helper functions.
Test Helpers
Extract repeated setup/verification into testXxx(t *testing.T, ...) helpers. Receive *testing.T as first argument and call t.Helper().
Benchmarks
Write func BenchmarkXxx(b *testing.B) for performance-critical code. Loop with b.N and use b.ResetTimer() to exclude setup time.
快速安装
/plugin add https://github.com/KubrickCode/ai-config-toolkit/tree/main/go-test在 Claude Code 中复制并粘贴此命令以安装该技能
GitHub 仓库
相关推荐技能
evaluating-llms-harness
测试该Skill通过60+个学术基准测试(如MMLU、GSM8K等)评估大语言模型质量,适用于模型对比、学术研究及训练进度追踪。它支持HuggingFace、vLLM和API接口,被EleutherAI等行业领先机构广泛采用。开发者可通过简单命令行快速对模型进行多任务批量评估。
issue-documentation
元该Skill为开发者提供标准化的issue文档模板和指南,适用于创建bug报告、GitHub/Linear/Jira问题等场景。它能系统化地记录问题状况、复现步骤、根本原因、解决方案和影响范围,确保团队沟通清晰高效。通过实施主流问题跟踪系统的最佳实践,帮助开发者生成结构完整的故障排除文档和事件报告。
llamaindex
元LlamaIndex是一个专门构建RAG应用的开发框架,提供300多种数据连接器用于文档摄取、索引和查询。它具备向量索引、查询引擎和智能代理等核心功能,支持构建文档问答、知识检索和聊天机器人等数据密集型应用。开发者可用它快速搭建连接私有数据与LLM的RAG管道。
generating-unit-tests
元该Skill能自动为源代码生成全面的单元测试,支持Jest、pytest、JUnit等多种测试框架。当开发者请求"生成测试"、"创建单元测试"或使用"gut"快捷指令时即可触发。它能智能识别合适框架或按指定框架生成测试用例,显著提升测试效率。
