單元測試是編寫有原則之Go程式中重要的一環。testing 套件提供了我們撰寫單元測試所需的工具,而 go test 指令會執行測試。
|
|
為了示範,此程式碼位於 main 套件中,但它可以位於任何套件中。測試程式碼通常與其所測試的程式碼位於同一個套件中。
|

package main
|
|
import (
"fmt"
"testing"
)
|
我們將測試一個簡單的整數最小值的實作。通常,我們所測試的程式碼會放在一個名為類似於 intutils.go 的來源檔案中,而其測試檔案則會命名為 intutils_test.go 。
|
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}
|
可透過撰寫一個函式並讓其名稱以 Test 開頭來建立測試。
|
func TestIntMinBasic(t *testing.T) {
ans := IntMin(2, -2)
if ans != -2 {
|
t.Error* 會回報測試失敗,但會繼續執行測試。t.Fatal* 會回報測試失敗,並立即停止測試。
|
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
|
撰寫測試可能會很重複,因此習慣上會使用「表格驅動程式」,其中會在表格中列出測試輸入和預期的輸出,然後使用一個單迴圈走訪它們並執行測試邏輯。
|
func TestIntMinTableDriven(t *testing.T) {
var tests = []struct {
a, b int
want int
}{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
|
t.Run 能夠執行「子測試」,每個表格項目執行一次。執行 go test -v 時會分別顯示它們。
|
for _, tt := range tests {
|
|
testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
ans := IntMin(tt.a, tt.b)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
|
基準測試通常會放在 _test.go 檔案中,並以 Benchmark 開頭命名。testing 執行器會數次執行每個基準測試函式,在每次執行時增加 b.N ,直到收集到精確的測量結果。
|
func BenchmarkIntMin(b *testing.B) {
|
基準測試通常會在迴圈中 b.N 次執行我們要進行基準測試的函式。
|
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}
}
|