文件目录:
被测试的函数放在Squre.go里面:
Squre.go
package Unittest
func Squre(op int) int {
return op * op
}
测试代码块:
Squre_test.go
package Unittest
import "testing"
func TestSqure(t *testing.T) {
inputs := [...]int{1, 2, 3}
expect := [...]int{1, 4, 9}
for i := 0; i < len(inputs); i++ {
ret := Squre(inputs[i])
if ret != expect[i] {
t.Errorf("input :%d,but actually is not expected: %d ", inputs[i], expect[i])
}
}
}
执行,进入Unittest目录:执行命令go test -v -cover
Unittest>go test -v -cover
Unittest>go test -v -cover
=== RUN TestSqure
--- PASS: TestSqure (0.00s)
PASS
coverage: 100.0% of statements
ok src/Unittest 0.040s