package main func Add (a, b int) int{ return a + b } func Multiply (a, b int) int { return a * b }
测试代码
package main import "testing" func TestAdd (t *testing.T) { c := Add(1, 2) if c != 3 { t.Fatalf("add(1, 2) expect 3, actual is %d", c) } t.Logf("test add succ") } func TestMultiply (t *testing.T) { a := Multiply(2, 5) b := Multiply(2, 3) if a != 10 { t.Fatalf("Multiply(2, 5) expect 10, actual %d", a) } if b != 6 { t.Fatalf("Multiply(2, 3) expect 6, actual %d", b) } t.Logf("test multiply succ")
1.文件名calc.go, 测试文件名calc_test.go
2.方法名Add, 测试该方法TestAdd(t *testing.T)
3.t.Fatalf 输出测试失败的语句
测试命令: go test
go test -v 可以具体执行了那些测试
测试通过
原文:https://www.cnblogs.com/zhangxiaoj/p/11267363.html