Skip to content
Latchkey

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.out or convert it as needed.

Terminal

Terminal
go test ./... -race -covermode=atomic -coverprofile=coverage.out
go tool cover -func=coverage.out | tail -n 1

Gotchas

  • Use -covermode=atomic when running with -race; set mode is not safe under concurrency.
  • By default coverage counts only the package under test; add -coverpkg=./... to count cross-package usage.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →