すべての関数にユニットテストを提供する
Contents
Provide unit tests for every function, and ensure all tests pass. (すべての関数にユニットテストを提供し、すべてのテストが合格することを保証する)
解説
ユニットテストは、コードの品質を保証し、リファクタリング時の安全性を提供する重要な要素です。すべての関数にテストがあることで、変更時に既存機能が破壊されていないことを迅速に検証できます。テストが存在しないコードは、変更のリスクが高く、技術的負債となります。MUST項目として、例外なくすべての関数にテストを記述し、継続的に実行して合格状態を維持する必要があります。
具体例
// 実装
func Add(a, b int) int {
return a + b
}
func ValidateEmail(email string) bool {
return strings.Contains(email, "@")
}
// テスト
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
want int
}{
{"positive numbers", 2, 3, 5},
{"negative numbers", -1, -2, -3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestValidateEmail(t *testing.T) {
if !ValidateEmail("test@example.com") {
t.Error("Valid email should pass")
}
if ValidateEmail("invalid") {
t.Error("Invalid email should fail")
}
}
参考リンク
すべての関数にユニットテストを提供する https://www.tricrow.com/core/coding-standard/unit-tests.html

