golang-testing

Solid

Go 测试模式,包括表格驱动测试、子测试、基准测试、模糊测试和测试覆盖率。遵循具有惯用 Go 实践的 TDD 方法论。

Testing & QA 368 stars 75 forks Updated 1 months ago MIT

Install

View on GitHub

Quality Score: 88/100

Stars 20%
85
Recency 20%
75
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Go 测试模式 (Testing Patterns) 遵循测试驱动开发(TDD)方法论,编写可靠且易于维护的 Go 测试的全面模式。 ## 何时激活 - 编写新的 Go 函数或方法时 - 为现有代码增加测试覆盖率时 - 为性能关键型代码创建基准测试(Benchmarks)时 - 为输入验证实现模糊测试(Fuzz tests)时 - 在 Go 项目中遵循 TDD 工作流时 ## Go 的 TDD 工作流 ### 红-绿-重构 (RED-GREEN-REFACTOR) 循环 ``` RED → 先编写一个失败的测试 GREEN → 编写最少的代码使测试通过 REFACTOR → 在保持测试通过的同时改进代码 REPEAT → 继续处理下一个需求 ``` ### Go 中的分步 TDD ```go // 步骤 1:定义接口/签名 // calculator.go package calculator func Add(a, b int) int { panic("not implemented") // 占位符 } // 步骤 2:编写失败的测试 (RED) // calculator_test.go package calculator import "testing" func TestAdd(t *testing.T) { got := Add(2, 3) want := 5 if got != want { t.Errorf("Add(2, 3) = %d; want %d", got, want) } } // 步骤 3:运行测试 - 验证失败 (FAIL) // $ go test // --- FAIL: TestAdd (0.00s) // panic: not implemented // 步骤 4:实现最少代码 (GREEN) func Add(a, b int) int { return a + b } // 步骤 5:运行测试 - 验证通过 (PASS) // $ go test // PASS // 步骤 6:如果需要则进行重构,并验证测试仍然通过 ``` ## 表格驱动测试 (Table-Driven Tests) Go 测试的标准模式。能够以最少的代码实现全面的覆盖。 ```go func TestAdd(t *testing.T) { tests := []struct { name string a, b int expected int }{ {"positive numbers", 2, 3, 5}, {"negative numbers", -1, -2, -3}, {"zero values", 0, 0, 0}, {"mixed signs", -1, 1, 0}, {"large numbers", 1000000, 2000000, 3000000}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := Add(tt.a, tt.b) if got !=...

Details

Author
xu-xiang
Repository
xu-xiang/everything-claude-code-zh
Created
2 months ago
Last Updated
1 months ago
Language
JavaScript
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

Testing & QA Featured

golang-benchmark

Golang benchmarking, profiling, and performance measurement. Use when writing, running, or comparing Go benchmarks, profiling hot paths with pprof, interpreting CPU/memory/trace profiles, analyzing results with benchstat, setting up CI benchmark regression detection, or investigating production performance with Prometheus runtime metrics. Also use when the developer needs deep analysis on a specific performance indicator - this skill provides the measurement methodology, while golang-performance provides the optimization patterns.

1,098 Updated 6 days ago
samber
Testing & QA Featured

swift-testing-expert

Expert guidance for Swift Testing: test structure, #expect/#require macros, traits and tags, parameterized tests, test plans, parallel execution, async waiting patterns, and XCTest migration. Use when writing new Swift tests, modernizing XCTest suites, debugging flaky tests, or improving test quality and maintainability in Apple-platform or Swift server projects.

334 Updated 6 days ago
AvdLee
AI & Automation Listed

check-coverage

Run test coverage measurement, analyze results, and fix gaps when coverage falls below the 80% threshold.

21 Updated 6 days ago
JetBrains
Testing & QA Solid

playwright-core

Battle-tested Playwright patterns for E2E, API, component, visual, accessibility, and security testing. Covers locators, assertions, fixtures, network mocking, auth flows, debugging, and framework recipes for React, Next.js, Vue, and Angular. TypeScript and JavaScript.

185 Updated 1 weeks ago
testdino-hq
Testing & QA Listed

e2e

Activate for any work in the tests/e2e/ directory: creating or editing test files (tests/*.test.ts), page objects (pages/), helpers (helpers/), or vitest config. Enforces agent-browser conventions specific to this project.

23 Updated 4 days ago
madarasz