Slide 24
Slide 24 text
ςετ
func Sum(list []int) int {
sum := 0
for _, i := range list {
sum += i
}
return sum
}
type sumTest struct {
in []int
out int
}
var sumTests = []sumTest{
{[]int{1, 2, 3}, 6},
{[]int{}, 0},
{[]int{-1, 1}, 1}, // Θ͟ͱ FAIL
}
func TestSum(t *testing.T) {
for i, test := range sumTests {
out := Sum(test.in)
if out != test.out {
t.Errorf("#%d Sum(%v) = %d, want %d",
i, test.in, out, test.out)
}
}
}
w ςετίʔυͷϑΝΠϧ໊
xxx_test.go YYYҙ
w ςετ໊ؔTestͰ࢝·Γ
Ҿʹ*testing.TΛड͚
Δ
w ཧpackageݩίʔυͱ
͢Δ _testΛ͚ͭΔ