comdiv
comdiv

Reputation: 941

How to correctly cover all golang packages with all tests?

Assume that some code defined under package pkg/somepkg1. And there are some tests in this package and some tests are in package tests/integration.

If i call

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

i have got all tests run (including integration) but there is no cover information in cover.out - just coverage from somepkg1 only! For example 78%

If i call

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

i have got exactly what i want - i see really all coverage for code is somepackage1 netherless it is from it's package or from integration. For example 85%.

So if want to check all packages in ./pkg i call same code for all packages and then merge answers in one batch. It takes long time and not good for CI

It's not consistent for me at all. I understand that fairy unit-tests are better than coverage thorough other modules and integration tests, but it's not friendly to use.

Is there way to cover all with all tests without calling such snippet against each package?

Upvotes: 2

Views: 532

Answers (2)

jayson mulwa
jayson mulwa

Reputation: 157

Use to test all packages indiscriminately:

 go test ./...

For all packages, you can add coverage as:

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

To test within a specific package would be:

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

Upvotes: 1

comdiv
comdiv

Reputation: 941

Solved!

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

in this case - it do exactly what i want and it's not same as

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

if you want to test group of packages you can do it with

go test -cover -coverpkg=./pkg/pkg1,./pkg/pkg2 -coverprofile=cover.out ./...

with commas and no spaces

Upvotes: 0

Related Questions