go test用法(获取ut覆盖率)
1 命令
1.1 go test ./… (运行当前目录及所有子目录下的测试用例)
# 运行当前目录及其子目录下的测试用例
go test ./...
1.2 go test dir0/…(运行指定目录及所有子目录用例)
# 运行dir0目录及其子目录下的用例
go test dir0/...
1.3 go test user…(运行user前缀的用例)
# 运行指定前缀的测试用例
go test user...
1.4 go test …(运行GOPATH下的所有用例)
# 运行GOPATH下的所有用例
go test ...
1.5 goland可视化执行
2 生成并分析覆盖率报告
2.1 生成报告
go test -mod=vendor -covermode=count -coverprofile=coverprofile.cov -run="^Test" -coverpkg=$(go list -mod=vendor ./... | grep -v "/test" | tr '\n' ',') ./...
或 非 vendor 模式:
go test -covermode=count -coverprofile=coverprofile.cov -run="^Test" -coverpkg=$(go list ./... | grep -v "/test" | tr '\n' ',') ./...
指令简单说明:
2.2 分析报告
①html方式查看:go tool cover -html=coverprofile.cov
# 通过html 文件打开(推荐,能看到方法细节):
go tool cover -html=coverprofile.cov
②命令行查看:go tool cover -func=coverprofile.cov
# 在命令行直接查看:
go tool cover -func=coverprofile.cov
参考:https://blog.csdn.net/xiaoliizi/article/details/107568024