One major new feature of
go test
is that @H_301_9@it can now compute and,with help from a new,separately installed “go tool cover
” program,display test coverage results.The
cover
tool is part of the 07002. It can be installed by running
$ go get golang.org/x/tools/cmd/cover
The cover tool does two things.
- First,when “
go test
” is given the-cover
flag,it is run automatically to rewrite the source for the package and insert instrumentation statements. The test is then compiled and run as usual,and basic coverage statistics are reported:
$ go test -coverprofile fmt ok fmt 0.060s coverage: 91.4% of statements $
Second,for more detailed reports,different flags to “go test” can create a coverage profile file,which the cover program,invoked with “
go tool cover
“,can then analyze.
The latest versions of Go (2013/09/19) use:
go test -coverprofile <filename> <package name>
Details on how to generate and analyze coverage statistics can be found by running the commands
$ go help testflag $ go tool cover -help
go test -coverprofile cover.out
and then
go tool cover -html=cover.out -o cover.html
openscover.html
in browser
我甚至不想等待浏览器打开,所以我定义了这个别名:
alias gc=grep -v -e " 1$" coverage.out
我只是键入gc,并有一个列表,所有的线尚未覆盖(这里:coverage.out行不以“1”结束)。