Home > Software engineering >  go test coverage failing to build with test only packages
go test coverage failing to build with test only packages

Time:11-08

When testing go files for go Test Coverage, over different packages i run this command -

Command got from this answer code coverage over different packages

go test --cover -covermode=count  -coverpkg=./app_test/...  ./... -coverprofile=cover.out

but it's giving me [build failed]

After running the command my output is like this -

go build integration-test/app_test/metadata: no non-test Go files in /home/farhad/GoCodes/GPS-server/integration-test/app_test/metadata
go build integration-test/app_test/ping: no non-test Go files in /home/farhad/GoCodes/GPS-server/integration-test/app_test/ping
go build integration-test/app_test/ID: no non-test Go files in /home/farhad/GoCodes/GPS-server/integration-test/app_test/ID
go build integration-test/app_test/history: no non-test Go files in /home/farhad/GoCodes/GPS-server/integration-test/app_test/history
?       integration-test        [no test files]
FAIL    integration-test/app_test/ID [build failed]
?       integration-test/app_test/common        [no test files]
FAIL    integration-test/app_test/history [build failed]
FAIL    integration-test/app_test/metadata [build failed]
FAIL    integration-test/app_test/ping [build failed]
?       integration-test/domain [no test files]
?       integration-test/errors [no test files]
FAIL

how to get coverage over different packages?

This is my package structure -

├── app_test
│   ├── common
│   │   └── common.go
│   ├── history
│   │   └── history_test.go
│   ├── ID
│   │   └── id_test.go
│   ├── metadata
│   │   └── metadata_test.go
│   └── ping
│       └── ping_test.go
├── data
│   ├── locations.json
│   └── metadata.json
├── docker-compose.yml
├── Dockerfile
├── domain
│   ├── company.go
│   ├── history.go
│   ├── location.go
│   └── metadata.go
├── errors
│   └── rest_error.go
├── ex.txt
├── go.mod
├── go.sum
├── history_config.local.json
├── id_config.local.json
├── main.go
├── Makefile
├── metadata_config.local.json
├── ping_config.local.json
├── README.md
├── run.sh

CodePudding user response:

This seems to be an issue that was fixed in release go 1.17.3 onwards because of cmd/go: test coverpkg=all ./... with test only packages will fail to build #27333

The gist of the issue is, packages failing to build if there are test only packages (without .go files) like in your case. There are couple of workarounds suggested in the thread.

Workaround - https://github.com/golang/go/issues/27333#issuecomment-770200300

I've sent a fix above, which will presumably make it into Go 1.17. Until then, I agree with previous posters that the easiest workaround is to avoid test-only packages by dropping a no-op non-test file in those packages. For example: echo "package foo" >foo/foo.go. That workaround can be undone when the fix above ships in a stable release.

CodePudding user response:

I highly recommend go-acc tool. That's how we do go accurate cross-package cover measurement.

So basically you will need just this:

$ goacc ./... -o=coverage.out
  • Related