Documentation ¶
Overview ¶
gocov is a replacement for the 'go test' command to be used for coverage.
It replicates the behavior of 'go test' as-is with one exception: it separates build and execution of the test binary.
Usage ¶
gocov [options] [patterns] -- [go test flags]
Arguments following '--' (if any) are passed to the test binary as-is. This means that flags you intended to pass to 'go test' should be prefixed with 'test.'.
For example:
# Instead of this: go test -run TestFoo ./... # Do this: gocov ./... -- -test.run TestFoo
gotestsum ¶
To use gocov with gotestsum, use the following command:
// Include other gocov flags as needed. gotestsum --raw-command -- gocov -test2json ./...
Why ¶
This is a workaround for Go's integration test coverage tracking functionality which, as of Go 1.21, does not yet support merging unit and integration test coverage in one run.
Specifically, if you build a coverage-instrumented binary and then also run tests with coverage tracking:
go build -o bin/whatever -cover ./cmd/whatever GOCOVERDIR=$someDir go test -cover
The GOCOVERDIR environment variable will NOT be propagated to invocations of bin/whatever made from inside the tests because 'go test' will override the environment variable. https://github.com/golang/go/blob/c19c4c566c63818dfd059b352e52c4710eecf14d/src/cmd/go/internal/test/test.go#L1337-L1341
To work around this, we need to build the test binary with 'go test' and then run it separately, setting GOCOVERDIR ourselves.
See https://github.com/golang/go/issues/51430#issuecomment-1344711300 and https://dustinspecker.com/posts/go-combined-unit-integration-code-coverage/ for more details on this workaround.