golang-testing
OfficialGo testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and test coverage. Follows TDD methodology with idiomatic Go practices.
What this skill does
When applied, it prepends a system prompt before your request is sent — no extra calls and no change to how you are billed beyond the added tokens.
---
name: golang-testing
description: Go testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and test coverage. Follows TDD methodology with idiomatic Go practices.
---
# Go 測試模式
用於撰寫可靠、可維護測試的完整 Go 測試模式,遵循 TDD 方法論。
## 何時啟用
- 撰寫新的 Go 函式或方法
- 為現有程式碼增加測試覆蓋率
- 為效能關鍵程式碼建立基準測試
- 實作輸入驗證的模糊測試
- 在 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:執行測試 - 驗證失敗
// $ go test
// --- FAIL: TestAdd (0.00s)
// panic: not implemented
// 步驟 4:實作最少程式碼(GREEN)
func Add(a, b int) int {
return a + b
}
// 步驟 5:執行測試 - 驗證通過
// $ go test
// PASS
// 步驟 6:如需要則重構,驗證測試仍然通過
```
## 表格驅動測試
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 != tt.expected {
t.Errorf("Add(%d, %d) = %d; want %d",
tt.a, tt.b, got, tt.expected)
}
})
}
}
```
### 帶錯誤案例的表格驅動測試
```go
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
input string
want *Config
wantErr bool
}{
{
name: "valid config",
input: `{"host":Use this skill
Add a "skill" field with the skill’s ID to your chat completion request. It is applied server-side before your prompt is sent — no extra calls.
{
"model": "gpt-4o-mini",
"skill": "imp-14973371-447b-43ff-afb4-a4039a6f532d",
"messages": [{ "role": "user", "content": "…" }]
}Install the skill, enable it in your dashboard and (optionally) limit it to specific models. It then applies automatically to every matching request — with no "skill" field to send each time.
Set it up in your dashboardMore skills
Delegate coding work to Codex, Claude Code, or OpenCode as background workers; not simple edits or read-only code lookup.
Create, edit, audit, tidy, validate, or restructure AgentSkills and SKILL.md files.
Search and analyze your own session logs (older/parent conversations) using jq.
Create, view, edit, delete, search, move, or export Apple Notes via the memo CLI on macOS.
Diagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
Set up and use 1Password CLI for sign-in, desktop integration, and reading or injecting secrets.
List, add, edit, complete, or delete Apple Reminders and reminder lists via remindctl.
Create, search, and manage Bear notes via grizzly CLI.