请注意,Go1.2(2013年第四季度,rc1可用)现在将显示
测试覆盖率结果 :
的一项主要新功能
go test是, 它现在可以计算并在单独安装的新程序的帮助下go tool cover显示测试覆盖率结果。该
cover工具是一部分go.toolssubrepository。可以通过运行安装
$ go get golang.org/x/tools/cmd/cover
封面工具有两件事。
- 首先,
go test给“ ”-cover标志时,它将自动运行以重写包的源并插入检测语句。然后将测试编译并照常运行,并报告基本覆盖率统计信息:
$ go test -coverprofile fmtcoverage.html fmtok fmt 0.060s coverage: 91.4% of statements$
其次,对于更详细的报告,要“进行测试”的不同标志可以创建一个覆盖率配置文件,
go tool cover然后用“ ” 调用的覆盖程序可以对其进行分析。
弗兰克·希勒(FrankShearar)提到:
Go(2013/09/19)的最新版本使用:
go test -coverprofile <filename> <package name>
运行命令可以找到有关如何生成和分析覆盖率统计信息的详细信息
$ go help testflag$ go tool cover -help
伊万·布莱克(IvanBlack)在评论中提到:
go test -coverprofile cover.out然后在您的默认浏览器中go tool cover -html=cover.out打开cover.out
我什至不想等待浏览器打开,所以我定义了这个别名:
alias gc=grep -v -e " 1$" cover.out
我只需要输入
gc,并拥有所有行的列表 不是 尚未覆盖的(这里用
coverage.out行 不 以“结束
1“)。



