Golang之单元测试
文件名必须以_test.go结尾
使用go test 执行单元测试
例
package main func add(a, b int) int { return a + b } func sub(a, b int) int { return a - b }
package main import ( "testing" ) func TestAdd(t *testing.T) { r := add(2, 4) if r != 6 { t.Fatalf("add(2,4) error,expect:%d,actual:%d", 6, r) } t.Logf("test add succ") } func TestSub(t *testing.T) { r := sub(2, 4) if r != -2 { t.Fatalf("sub(2,4) error,expect:%d,actual:%d", 6, r) } t.Logf("test sub succ") }
package main
运行:
E:\project\src\go_dev\day8\test>go test -v === RUN TestAdd --- PASS: TestAdd (0.00s) calc_test.go:13: test add succ === RUN TestSub --- PASS: TestSub (0.00s) calc_test.go:20: test sub succ PASS ok go_dev/day8/test 0.142s
努力成为一个开发者
个人站点:www.pythonav.cn