golang test模块
1 创建文件夹testing101
2 被测试程序
package testing101 func Sum(numbers []int) int { sum :=0 for _,n :=range numbers { sum +=n } return sum }
3 测试程序
package testing101 import ( "testing" ) func TestSum(t *testing.T) { numbers := []int{1, 2, 3, 4, 5} expected := 15 actual := Sum(numbers) if actual != expected { t.Errorf("Expected the sum of %v to be %d but instead got %d!", numbers, expected, actual) } }
测试结果
[ `go test -v` | done: 302.896837ms ]go test -v === RUN TestSum --- PASS: TestSum (0.00s) PASS ok testing101 0.005s [ ~/mysource/go/src/testing101/ ] #