Go语言测试(Test)&性能测试(Benchmark)
文章目录
简介
Test
1 | Package testing provides support for automated testing of Go packages. It is intended to be used in concert with the “go test” command, which automates execution of any function of the form. |
testing 包提供了对Go包的自动测试支持。 这是和go test 命令相呼应的功能, go test 命令会自动执行所以符合格式
func TestXXX(t *testing.T)
Benchmark
1 | Functions of the form |
符合格式 (见上)的函数被认为是一个性能测试程序, 当带着 -bench=“.” ( 参数必须有!)来执行*go test命令的时候性能测试程序就会被顺序执行。
简单的实例代码
lib.go
1 | package lib |
lib_test.go
1 | package lib |
执行 go test -bench=”.” 后 结果 :
1 | // 表示测试全部通过 |
Go testing 库 testing.T 和 testing.B 简介
testing.T
- 判定失败接口
- Fail 失败继续
- FailNow 失败终止
- 打印信息接口
- Log 数据流(cout 类似)
- Logf format (printf 类似)
- SkipNow 跳过当前测试
- Skiped 检测是否跳过
综合接口产生
- Error / Errorf 报告出错继续 [ Log / Logf + Fail ]
- Fatel / Fatelf 报告出错终止 [ Log / Logf + FailNow ]
- Skip / Skipf 报告并跳过 [ Log / Logf + SkipNow ]
testing.B
- testing.B 拥有testing.T 的全部接口
- SetBytes(i uint64) 统计内存消耗, 如果你需要的话
- SetParallelism(p int) 制定并行数目
- StartTimer / StopTimer / ResertTimer 操作计时器
testing.PB
- Next()接口。判断是否继续循环
本文引自这里