ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
_test结尾。默认的情况下,go test 命令不需要任何的参数,它会自动把你源码包下面所有 test 文件测试完毕,当然你也可以带上参数。Test为前缀,例如:
func TestXXX( t *testing.T )
go test 指令来执行,没有也不需要 main() 作为函数入口。所有在以_test结尾的源码内以Test开头的函数会自动被执行。./src/chapter11/gotest/helloworld_test.go):
本套教程所有源码下载地址:https://pan.baidu.com/s/1ORFVTOLEYYqDhRzeq0zIiQ 提取密码:hfyf
package code11_3
import "testing"
func TestHelloWorld(t *testing.T) {
t.Log("hello world")
}
代码说明如下:
$ go test helloworld_test.go
ok command-line-arguments 0.003s
$ go test -v helloworld_test.go
=== RUN TestHelloWorld
--- PASS: TestHelloWorld (0.00s)
helloworld_test.go:8: hello world
PASS
ok command-line-arguments 0.004s
代码说明如下:
-v,可以让测试时显示详细的流程。go test 指定文件时默认执行文件内的所有测试用例。可以使用-run参数选择需要的测试用例单独执行,参考下面的代码。./src/chapter11/gotest/select_test.go)
package code11_3
import "testing"
func TestA(t *testing.T) {
t.Log("A")
}
func TestAK(t *testing.T) {
t.Log("AK")
}
func TestB(t *testing.T) {
t.Log("B")
}
func TestC(t *testing.T) {
t.Log("C")
}
这里指定 TestA 进行测试:
$ go test -v -run TestA select_test.go
=== RUN TestA
--- PASS: TestA (0.00s)
select_test.go:6: A
=== RUN TestAK
--- PASS: TestAK (0.00s)
select_test.go:10: AK
PASS
ok command-line-arguments 0.003s
TestA 和 TestAK 的测试用例都被执行,原因是-run跟随的测试用例的名称支持正则表达式,使用-run TestA$即可只执行 TestA 测试用例。
./src/chapter11/gotest/fail_test.go)
func TestFailNow(t *testing.T) {
t.FailNow()
}
还有一种只标记错误不终止测试的方法,代码如下:
func TestFail(t *testing.T) {
fmt.Println("before fail")
t.Fail()
fmt.Println("after fail")
}
测试结果如下:
=== RUN TestFail before fail after fail --- FAIL: TestFail (0.00s) FAIL exit status 1 FAIL command-line-arguments 0.002s从日志中看出,第 5 行调用 Fail() 后测试结果标记为失败,但是第 7 行依然被程序执行了。
| 方 法 | 备 注 |
|---|---|
| Log | 打印日志,同时结束测试 |
| Logf | 格式化打印日志,同时结束测试 |
| Error | 打印错误日志,同时结束测试 |
| Errorf | 格式化打印错误日志,同时结束测试 |
| Fatal | 打印致命日志,同时结束测试 |
| Fatalf | 格式化打印致命日志,同时结束测试 |
./src/chapter11/gotest/benchmark_test.go)
package code11_3
import "testing"
func Benchmark_Add(b *testing.B) {
var n int
for i := 0; i < b.N; i++ {
n++
}
}
这段代码使用基准测试框架测试加法性能。第 7 行中的 b.N 由基准测试框架提供。测试代码需要保证函数可重入性及无状态,也就是说,测试代码不使用全局变量等带有记忆性质的数据结构。避免多次运行同一段代码时的环境不一致,不能假设 N 值范围。$ go test -v -bench=. benchmark_test.go goos: linux goarch: amd64 Benchmark_Add-4 20000000 0.33 ns/op PASS ok command-line-arguments 0.700s代码说明如下:
-bench=.表示运行 benchmark_test.go 文件里的所有基准测试,和单元测试中的-run类似。-bench=.应写为-bench="."。
-benchtime参数可以自定义测试时间,例如:
$ go test -v -bench=. -benchtime=5s benchmark_test.go goos: linux goarch: amd64 Benchmark_Add-4 10000000000 0.33 ns/op PASS ok command-line-arguments 3.380s
func Benchmark_Alloc(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("%d", i)
}
}
在命令行中添加-benchmem参数以显示内存分配情况,参见下面的指令:
$ go test -v -bench=Alloc -benchmem benchmark_test.go goos: linux goarch: amd64 Benchmark_Alloc-4 20000000 109 ns/op 16 B/op 2 allocs/op PASS ok command-line-arguments 2.311s代码说明如下:
-bench后添加了 Alloc,指定只测试 Benchmark_Alloc() 函数。./src/chapter11/gotest/benchmark_test.go):
func Benchmark_Add_TimerControl(b *testing.B) {
// 重置计时器
b.ResetTimer()
// 停止计时器
b.StopTimer()
// 开始计时器
b.StartTimer()
var n int
for i := 0; i < b.N; i++ {
n++
}
}
从 Benchmark() 函数开始,Timer 就开始计数。StopTimer() 可以停止这个计数过程,做一些耗时的操作,通过 StartTimer() 重新开始计时。ResetTimer() 可以重置计数器的数据。
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有