SupercovBlog

Guide

Go test coverage exclude: files, packages and mocks

Go has no exclude flag or ignore comment. Two ways that work, measured on Go 1.27.4, and what does not.

Go has no exclude flag. A brushed-aluminum tray of white tiles with two pockets empty, their violet tiles lifted out beside it.

Go has no exclude flag and no ignore comment. Choose the packages to measure with -coverpkg, or delete lines from the profile with grep before you read it. Files marked // Code generated … DO NOT EDIT. are not left out for you.

Exclude patterns at a glance

To excludeDo
Packages, such as mocks-coverpkg=$(go list ./... | grep -v /mocks | paste -sd, -)
Files or foldersgrep -v -E '/(mocks|gen)/' cover.out > cover.filtered.out
Generated filesFilter them the same way; Go does not skip them
Packages with no testsLeave them out of the go test package list
One function or lineMove it to a file you filter out

We ran every setup here with Go 1.27.4. The module has five packages: pricing with three tests, a generated gen package, mocks used by the tests, an unused legacy package and a cmd/shop main.

Choose the packages to measure

go test -coverprofile=cover.out ./...

This counted each package only through its own tests. pricing had 8 of 9 statements; the four packages with no tests showed at 0%, even the gen and mocks code the pricing tests ran. Total: 8 of 20 (40%).

go test -coverpkg=./... -coverprofile=cover.out ./...

-coverpkg counts a package whenever any test runs it. Now gen had 1 of 2 statements and mocks 3 of 5: 12 of 20.

To leave packages out, list the ones to keep:

go test -coverpkg=$(go list ./... | grep -v -e /mocks -e /cmd/ | paste -sd, -) \
  -coverprofile=cover.out ./...

The profile then had pricing, gen and legacy: 9 of 12 statements.

Bars for four ways to run the same tests: go test -coverprofile, 8 of 20 statements; -coverpkg=./..., 12 of 20; -coverpkg without mocks and cmd, 9 of 12; -coverpkg=./... then grep -v mocks and gen, 8 of 13.
The same three tests, four profiles.

Filter the profile

A coverage profile is a text file with one line per code block, starting with the file path. Delete the lines you do not want, then report:

go test -coverpkg=./... -coverprofile=cover.out ./...
grep -v -E '/(mocks|gen)/' cover.out > cover.filtered.out
go tool cover -func=cover.filtered.out

go tool cover -func printed 61.5%: 8 of 13 statements, without the mocks and the generated code. Keep the first line of the file, mode: set; a grep -v on paths leaves it in place. Codecov and other services accept the filtered file.

Why your Go coverage exclude isn’t working

What we triedWhat happened
A // Code generated … DO NOT EDIT. headerStill counted
go test ./... without -coverpkgCode run from another package’s tests counted as 0%
go test ./pricing/...Only pricing measured; the rest simply missing
An ignore comment such as // coverage:ignoreGo has no such comment
A grid of six setups against five packages. go test ./... and -coverpkg=./... keep all five. A -coverpkg list without mocks and cmd keeps pricing, gen and legacy. grep -v mocks and gen keeps pricing, legacy and cmd. go test ./pricing/... keeps only pricing. A DO NOT EDIT header on gen keeps all five.
Which packages each setup left in the profile, measured with Go 1.27.4.

Lines inside a function cannot be excluded. If a block must not count, move it into its own file, such as debug_gen.go, and filter that file out.

What Supercov adds

go run github.com/supercorp-ai/supercov/cmd/supercov@latest -- go test ./...
go run github.com/supercorp-ai/supercov/cmd/supercov@latest runs latest gaps

Supercov counts code in every package your tests reach, as -coverpkg=./... does, and records which test covered each line, with branches and MC/DC, which go test -cover does not report. It decides what counts from your source directories. See Go test coverage for the tools side by side and Go covermode for set, count and atomic.

More coverage guides