Go testing patterns including table-driven tests, subtests, benchmarks, fuzzing, and test coverage. Follows TDD methodology with idiomatic Go practices.
func BenchmarkProcess(b *testing.B) {
data := generateTestData(1000)
b.ResetTimer() // 不計算設置時間
for i := 0; i < b.N; i++ {
Process(data)
}
}
// 執行:go test -bench=BenchmarkProcess -benchmem
// 輸出:BenchmarkProcess-8 10000 105234 ns/op 4096 B/op 10 allocs/op
不同大小的基準測試
func BenchmarkSort(b *testing.B) {
sizes := []int{100, 1000, 10000, 100000}
for _, size := range sizes {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
data := generateRandomSlice(size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// 複製以避免排序已排序的資料
tmp := make([]int, len(data))
copy(tmp, data)
sort.Ints(tmp)
}
})
}
}
記憶體分配基準測試
func BenchmarkStringConcat(b *testing.B) {
parts := []string{"hello", "world", "foo", "bar", "baz"}
b.Run("plus", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var s string
for _, p := range parts {
s += p
}
_ = s
}
})
b.Run("builder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var sb strings.Builder
for _, p := range parts {
sb.WriteString(p)
}
_ = sb.String()
}
})
b.Run("join", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.Join(parts, "")
}
})
}
模糊測試(Go 1.18+)
基本模糊測試
func FuzzParseJSON(f *testing.F) {
// 新增種子語料庫
f.Add(`{"name": "test"}`)
f.Add(`{"count": 123}`)
f.Add(`[]`)
f.Add(`""`)
f.Fuzz(func(t *testing.T, input string) {
var result map[string]interface{}
err := json.Unmarshal([]byte(input), &result)
if err != nil {
// 隨機輸入預期會有無效 JSON
return
}
// 如果解析成功,重新編碼應該可行
_, err = json.Marshal(result)
if err != nil {
t.Errorf("Marshal failed after successful Unmarshal: %v", err)
}
})
}
// 執行:go test -fuzz=FuzzParseJSON -fuzztime=30s
多輸入模糊測試
func FuzzCompare(f *testing.F) {
f.Add("hello", "world")
f.Add("", "")
f.Add("abc", "abc")
f.Fuzz(func(t *testing.T, a, b string) {
result := Compare(a, b)
// 屬性:Compare(a, a) 應該總是等於 0
if a == b && result != 0 {
t.Errorf("Compare(%q, %q) = %d; want 0", a, b, result)
}
// 屬性:Compare(a, b) 和 Compare(b, a) 應該有相反符號
reverse := Compare(b, a)
if (result > 0 && reverse >= 0) || (result < 0 && reverse <= 0) {
if result != 0 || reverse != 0 {
t.Errorf("Compare(%q, %q) = %d, Compare(%q, %q) = %d; inconsistent",
a, b, result, b, a, reverse)
}
}
})
}
測試覆蓋率
執行覆蓋率
# 基本覆蓋率
go test -cover ./...
# 產生覆蓋率 profile
go test -coverprofile=coverage.out ./...
# 在瀏覽器查看覆蓋率
go tool cover -html=coverage.out
# 按函式查看覆蓋率
go tool cover -func=coverage.out
# 含競態偵測的覆蓋率
go test -race -coverprofile=coverage.out ./...
覆蓋率目標
程式碼類型
目標
關鍵業務邏輯
100%
公開 API
90%+
一般程式碼
80%+
產生的程式碼
排除
HTTP Handler 測試
func TestHealthHandler(t *testing.T) {
// 建立請求
req := httptest.NewRequest(http.MethodGet, "/health", nil)
w := httptest.NewRecorder()
// 呼叫 handler
HealthHandler(w, req)
// 檢查回應
resp := w.Result()
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("got status %d; want %d", resp.StatusCode, http.StatusOK)
}
body, _ := io.ReadAll(resp.Body)
if string(body) != "OK" {
t.Errorf("got body %q; want %q", body, "OK")
}
}
func TestAPIHandler(t *testing.T) {
tests := []struct {
name string
method string
path string
body string
wantStatus int
wantBody string
}{
{
name: "get user",
method: http.MethodGet,
path: "/users/123",
wantStatus: http.StatusOK,
wantBody: `{"id":"123","name":"Alice"}`,
},
{
name: "not found",
method: http.MethodGet,
path: "/users/999",
wantStatus: http.StatusNotFound,
},
{
name: "create user",
method: http.MethodPost,
path: "/users",
body: `{"name":"Bob"}`,
wantStatus: http.StatusCreated,
},
}
handler := NewAPIHandler()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body io.Reader
if tt.body != "" {
body = strings.NewReader(tt.body)
}
req := httptest.NewRequest(tt.method, tt.path, body)
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != tt.wantStatus {
t.Errorf("got status %d; want %d", w.Code, tt.wantStatus)
}
if tt.wantBody != "" && w.Body.String() != tt.wantBody {
t.Errorf("got body %q; want %q", w.Body.String(), tt.wantBody)
}
})
}
}
測試指令
# 執行所有測試
go test ./...
# 執行詳細輸出的測試
go test -v ./...
# 執行特定測試
go test -run TestAdd ./...
# 執行匹配模式的測試
go test -run "TestUser/Create" ./...
# 執行帶競態偵測器的測試
go test -race ./...
# 執行帶覆蓋率的測試
go test -cover -coverprofile=coverage.out ./...
# 只執行短測試
go test -short ./...
# 執行帶逾時的測試
go test -timeout 30s ./...
# 執行基準測試
go test -bench=. -benchmem ./...
# 執行模糊測試
go test -fuzz=FuzzParse -fuzztime=30s ./...
# 計算測試執行次數(用於偵測不穩定測試)
go test -count=10 ./...