Short answer. To see correct coverages run:
go test ./... -coverprofile=coverage.out -coverpkg=./...
go tool cover -html=coverage.out
And then see percentages in dropdown inside browser.
Longer answer with explanation why other ways not worked. So, I also have a project with api
and store
(in this question it is called db
) packages, where store
is used in api
, and I have tests in api
.
go test -cover ./...
Says that api
is covered, store
- "[no test files]".
When I run it like
go test -cover -coverpkg=./... ./...
It decreases coverage in api
because now that number means how testcases in api
package cover all the packages, which is not very useful number.
But there is a way to know how given package is covered by tests in all the packages (eg, how store
is covered by tests from api
and other places).
First, you generate coverage profile:
go test ./... -coverprofile=coverage.out -coverpkg=./...
Above command will still give you wrong percents in output, but it will create coverage.out
file that will list all the lines in all packages covered by tests in all packages.
Then you could view this report in the browser with the command:
go tool cover -html=coverage.out
Then in the dropdown that allows you to select files to browse coverage for, you will get correct per file coverages.