funcFib(n int)int{if n <2{return n
}returnFib(n-1)+Fib(n-2)}
测试代码
funcTestFib(t *testing.T){var(
in =7
expected =13)
actual :=Fib(in)if actual != expected {
t.Errorf("Fib(%d) = %d; expected %d", in, actual, expected)}}
执行 go test .,输出:
$ go test .
ok chapter09/testing 0.007s // 表示通过
修改函数为
funcFib(n int)int{if n <2{return n
}returnFib(n-1)+Fib(n-1)}
再次执行测试 go test . 得到输出 输出结果包括:出错的测试函数名称,执行时长和错误信息。
$ go test .--- FAIL: TestSum (0.00s)
t_test.go:16:Fib(10)=64; expected 13
FAIL
FAIL chapter09/testing 0.009s