How to Generate Go Coverage in CI
go test -coverprofile writes a coverage profile that go tool cover turns into a percentage or HTML.
Run go test ./... -coverprofile=coverage.out -covermode=atomic, then go tool cover -func=coverage.out to print the total. The .out profile is what upload tools consume.
Steps
- Run
go test ./... -coverprofile=coverage.out -covermode=atomic. - Print the total with
go tool cover -func=coverage.out. - Upload
coverage.outor convert it as needed.
Terminal
Terminal
go test ./... -race -covermode=atomic -coverprofile=coverage.out
go tool cover -func=coverage.out | tail -n 1Gotchas
- Use
-covermode=atomicwhen running with-race;setmode is not safe under concurrency. - By default coverage counts only the package under test; add
-coverpkg=./...to count cross-package usage.
Related guides
How to Enforce a Go Coverage Threshold in CIGate Go coverage in CI by parsing go tool cover output or using go-test-coverage, failing the job when total…
How to Upload Coverage to Codecov in CISend a coverage report to Codecov from CI with codecov/codecov-action, providing the token so private repos a…
How to Combine Coverage From a Matrix or Sharded Run in CIMerge coverage from parallel matrix or sharded CI jobs by uploading each shard as an artifact, then combining…