In a previous post I blogged about the cover
tool coming in Go 1.2 and a bash helper function I use to make the tool a little easier to use.
Since then I’ve extended these helpers so I wanted to blog about the improvements.
Passing arguments to the testing tool
cover () { t=$(tempfile) go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -func=$t && unlink $t }
The first improvement is to be able to pass arguments to go test
, this is done by setting COVERFLAGS
before invoking the cover
helper.
As an example, I have defined a flag in the sftp package tests that allow me to switch the integration tests on and off.
% cover github.com/pkg/sftp PASS coverage: 20.4% of statements ... % COVERFLAGS=-integration cover github.com/pkg/sftp PASS coverage: 69.2% of statements ...
Viewing coverage details
The cover
in its default mode will give you a percentage coverage on a per function basis, but it doesn’t tell you which parts of the function are uncovered.
cover-web() { t=$(tempfile) go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -html=$t && unlink $t }
To explore the coverage report in detail, this helper runs the cover
tool then opens a web browser with a html version of the coverage report.