英文:
Is it possible to compile a Go program with specific flags for coverage analysis?
问题
可以使用特定的标志来编译Go程序以进行覆盖率分析吗?
使用案例:
- 编译一个应用程序;
- 运行功能自动化测试;
- 分析覆盖率;
应该类似于Gcov或Python coverage。
非常感谢!
英文:
Is it possible to compile a Go program with specific flags for coverage analysis?
The use case:
- Compile an app;
- Run functional automated tests;
- Analyse coverage;
Should be something similar to Gcov or Python coverage.
Many Thanks!
答案1
得分: 6
是的,Go语言在测试过程中有一个名为"cover"的工具(从1.2版本开始)。只使用go test
命令会编译你的程序并运行任何自动化测试。添加-cover
标志将提供测试覆盖率的统计信息。
运行命令如下:
go test -cover
你还可以输出一个覆盖率文件:
go test -coverprofile=coverage.out
然后使用以下命令查看:
go tool cover -func=coverage.out
或者
go tool cover -html=coverage.out
以HTML格式输出(带有颜色编码)。
更多信息请参考http://blog.golang.org/cover,以及运行go tool cover -h
和go help testflag
命令。
英文:
Yes, Go has the cover tool (as of version 1.2) incorporated into the test process. go test
alone will compile your program and run any automated tests you may have. Adding the -cover
flag will provide statistics on test coverage.
To run it:
go test -cover
You can also output a coverage profile:
go test -coverprofile=coverage.out
and then view it with:
go tool cover -func=coverage.out
or
go tool cover -html=coverage.out
for HTML formatted output (with colour coding).
See http://blog.golang.org/cover , go tool cover -h
and go help testflag
for more info.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论