go-单元测试-基准测试
go-单元测试-基准测试
使用
文件名后后缀为
_test
- 一般把 测试文件 丢带被 测试的方法 同包名 下. (构建 main 程序执行时, 不会执行到
_test
文件中的init
方法) - 单元测试, 要求函数名以
Test
开头, 参数为*testing.T
- 性能测试, 要求函数名以
Benchmark
开头, 参数为*testing.B
Test
/Benchmark
开头后面紧接着_
或 大写字母开头的单词, 如- 合法: TestRun (建议), Test_run
- 不合法: Testrun
- 一般把 测试文件 丢带被 测试的方法 同包名 下. (构建 main 程序执行时, 不会执行到
待测试文件 cat.go
1
2
3
4
5
6
7package cat
import "log"
func Run(speed int) {
log.Printf("--- Run, speed:%+v\n", speed)
}单元测试文件 cat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13package cat
import (
"testing"
)
func TestRun(t *testing.T) { // 单元测试, 要求函数名以 Test 开头, 参数为 *testing.T
Run(111)
}
func BenchmarkTransport1(b *testing.B) { // 性能测试, 要求函数名以 Benchmark 开头, 参数为 *testing.B
Run(222)
}
在 vscode 中直接可以点击测试
在 goland 中右键点击测试
单元测试
- go test单元测试 - http://objcoding.com/2018/09/14/go-test/
- Go 单元测试 - https://www.flysnow.org/2017/05/16/go-in-action-go-unit-test.html
- testing - 单元测试 - https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter09/09.1.html
命令
测试指定文件
cd 到测试文件目录下执行命令
1 | f:\a_link_workspace\go\GoWinEnv_new\src\GoLab\test_uuid (master -> origin) |
$ go test -v
: 测试所有文件$ go test -v file1.go file2.go
: 测试指定文件
测试指定方法
cd 到测试文件目录下执行命令
1 | f:\a_link_workspace\go\GoWinEnv_Mars\src\mars\test\rpc (master -> origin) |
^Test_002$
测试所有文件的所有匹配的方法- 可用参数
- -timeout d : 默认 d 是 10m0s; 如果设为 0, 则表示不超时
测试结果 json 化
输出 json 格式的结果, 在测试方法后面加上
-json
标记1
2
3
4$ go test -v -timeout 0 -run Test_001 -json
{"Time":"2020-11-03T10:35:42.1270576+08:00","Action":"run","Package":"go-lab/test_flag","Test":"Test_001"}
...
{"Time":"2020-11-03T10:35:42.1758557+08:00","Action":"pass","Package":"go-lab/test_flag","Elapsed":0.389}
命令行参数
方法
1
2
3
4
5
6
7
8
9
10func Test_Args(t *testing.T) {
if !flag.Parsed() {
flag.Parse()
}
argList := flag.Args() // flag.Args() 返回 -args 后面的所有参数,以切片表示,每个元素代表一个参数
for _, arg := range argList {
fmt.Printf("--- arg: %s\n", arg)
}
}
直接执行测试方法
命令, 在 测试方法后面加入
-args
标识及参数1
2
3
4
5
6$ go test -v -timeout 0 -run Test_Args -args aaa bbb ccc
=== RUN Test_Args
--- arg: aaa
--- arg: bbb
--- arg: ccc
--- PASS: Test_Args (0.00s)
构建二进制后再执行测试方法
命令
1
2
3
4
5
6
7
8$ go test -c // 构建 test_flag.test.exe 程序
$ .\test_flag.test.exe -test.v -test.run Test_Args aaa bbb ccc
=== RUN Test_Args
--- arg: aaa
--- arg: bbb
--- arg: ccc
--- PASS: Test_Args (0.00s)
基准测试
- Go 基准测试 - https://www.flysnow.org/2017/05/21/go-in-action-go-benchmark-test.html
- Go 语言测试(Test)、性能测试(Benchmark) 学习笔记 - https://blog.csdn.net/cchd0001/article/details/48181239
- testing - 基准测试 - https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter09/09.2.html
- Go语言性能测试 - https://www.cnblogs.com/davygeek/p/7741616.html
- testing - 基准测试 - https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter09/09.2.html
命令
测试当前目录所有文件所有方法
go test -bench=. -benchtime=3s
测试指定文件指定方法
go test benchmark_test.go -bench=Benchmark_Cast -benchtime=3s
测试用例 benchmark_test.go 文件
1 | func Benchmark_Cast(b *testing.B) { |
测试
1 | F:\a_link_workspace\go\GoWinEnv_new\src\GoLab\test_unit_benchmark (master -> origin) |
- -count=2 : 运行 2 次
- -cpu=8 : The
8
后缀和用于运行次测试的GOMAXPROCS
值有关。 与GOMAXPROCS
一样,此数字默认为启动时Go进程可见的CPU数。 你可以使用-cpu
标识更改此值,可以传入多个值以列表形式来运行基准测试。
Cpu Profile
生成 profile 文件
1 | $ go test -run=xxx -bench=. -benchtime=3s -cpuprofile profile_cpu.out |
xxx 就是 xxx, 不代表什么反方, xxx 就对了
该命令会跳过单元测试,执行所有benchmark,同时生成一个cpu性能描述文件.
查看 profile 文件
1 | $ go tool pprof app.test profile_cpu.out |
build 单元测试 可执行文件
有了这个就可以不用单独又写一个 main 包来构建 可执行文件, 直接构建 测试用例 并 执行
参考: https://deepzz.com/post/the-command-flag-of-go-test.html
比如: pc 环境下, 测试用例 所在 包名 为:
test_http
,
测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14package test_http
import (
"log"
"testing"
)
func Test_fileSrv01(t *testing.T) {
log.Printf("--- Test_fileSrv01\n")
}
func Test_fileSrv02(t *testing.T) {
log.Printf("--- Test_fileSrv02\n")
}cd 到 测试用例 所在目录, 执行命令:
go test -c
. 编译测试二进制文件为[pkg].test
,不运行测试。1
2I:\workspace\go\go-lab\test_net\test_http (master -> origin)
$ go test -c这里则会生成
test_http.test.exe
执行可执行文件里的测试用例, 执行命令:
1
2
3$ .\test_http.test.exe -test.run ^Test_fileSrv02$
2020/10/18 13:36:49 --- Test_fileSrv02
PASS
执行带参数的可执行文件
代码
1
2
3
4
5
6
7
8
9
10var (
a = flag.Int("a", 0, "first number")
b = flag.Int("b", 0, "second number")
expected = flag.Int("expected", 0, "expected result")
)
func TestRummyFileSrv(t *testing.T) {
flag.Parse()
fmt.Printf("a: %d, b: %d\n", *a, *b)
}构建可执行文件
1
$ go test -c
传入参数并执行,
-test.v -a=1 -b=2
就是传入参数的示例1
2
3
4
5$ .\test_game.test.exe -test.v -a=1 -b=2 -test.run ^TestRummyFileSrv
=== RUN TestRummyFileSrv
a: 1, b: 2
--- PASS: TestRummyFileSrv (0.00s)
PASS