Go coverage with external tests
The Go test coverage implementation is quite ingenious: when asked to, the Go compiler will preprocess the source so that when each code portion is executed a bit is set in a coverage bitmap. This is integrated in the go test
tool: go test -cover
enables it and -coverprofile=
allows you to write a profile to then inspect with go tool cover
.
This makes it very easy to get unit test coverage, but there's no simple way to get coverage data for tests that you run against the main version of your program, like end-to-end tests.
The proper fix would involve adding -cover
preprocessing support to go build
, and exposing the coverage profile maybe as a runtime/pprof.Profile
, but as of Go 1.6 there’s no such support. Here instead is a hack we've been using for a while in the test suite of RRDNS, our custom Go DNS server.
We create a dummy test that executes main()
, we put it behind a build tag, compile a binary with go test -c -cover
and then run only that test instead of running the regular binary.
Here's what the rrdns_test.go
file looks like:
// +build Continue reading