标签:package 0.00 cte tin success 单元测试 imp i++ log
package testcase func sum(n int) int{ sum := 0 for i:=1;i<=n;i++{ sum += i } return sum } func sub(a,b int)int{ return a -b }
package testcase import "testing" func TestSumCase(t *testing.T){ res := sum(10) if res != 55{ t.Fatalf("error occurred, expected %d, got %d",55,res) } t.Logf("successfully executed") }
package testcase import "testing" func TestSubCase(t *testing.T){ res := sub(10, 3) if res != 7{ t.Fatalf("error occurred,expected %d,got %d",7, res) } t.Logf("successfully executed") }
C:\Users\Administrator\go\src\awesomeProject\testcase>go test -v === RUN TestSubCase --- PASS: TestSubCase (0.00s) sub_test.go:10: successfully executed === RUN TestSumCase --- PASS: TestSumCase (0.00s) sum_test.go:11: successfully executed PASS ok awesomeProject/testcase 0.061s
这是测试全部,如果只是测试单个文件,go test -v sum_test.go cal.go
测试单个方法,go test -v -test.run TestSubCase
标签:package 0.00 cte tin success 单元测试 imp i++ log
原文地址:https://www.cnblogs.com/traditional/p/10027983.html