测试覆盖率构建失败,只能用于测试的包。

huangapple go评论94阅读模式
英文:

go test coverage failing to build with test only packages

问题

在测试Go文件的测试覆盖率时,针对不同的包,您可以运行以下命令:

从这个答案中获取的命令 code coverage over different packages

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

但是它给出了[build failed]的错误提示。

运行该命令后,输出如下:

go build integration-test/app_test/metadata: 在 /home/farhad/GoCodes/GPS-server/integration-test/app_test/metadata 中没有非测试的Go文件
go build integration-test/app_test/ping: 在 /home/farhad/GoCodes/GPS-server/integration-test/app_test/ping 中没有非测试的Go文件
go build integration-test/app_test/ID: 在 /home/farhad/GoCodes/GPS-server/integration-test/app_test/ID 中没有非测试的Go文件
go build integration-test/app_test/history: 在 /home/farhad/GoCodes/GPS-server/integration-test/app_test/history 中没有非测试的Go文件
?       integration-test        [没有测试文件]
FAIL    integration-test/app_test/ID [构建失败]
?       integration-test/app_test/common        [没有测试文件]
FAIL    integration-test/app_test/history [构建失败]
FAIL    integration-test/app_test/metadata [构建失败]
FAIL    integration-test/app_test/ping [构建失败]
?       integration-test/domain [没有测试文件]
?       integration-test/errors [没有测试文件]
FAIL

如何在不同的包上获取覆盖率?

这是我的包结构:

├── 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
英文:

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

答案1

得分: 1

这似乎是一个在go 1.17.3及以后版本中修复的问题,原因是cmd/go: test coverpkg=all ./... with test only packages will fail to build #27333

问题的要点是,如果存在只有测试文件(没有.go文件)的测试包,那么这些包将无法构建。在讨论中提出了几种解决方法。

解决方法 - https://github.com/golang/go/issues/27333#issuecomment-770200300

> 我已经提供了一个修复方法,预计将包含在Go 1.17中。在那之前,我同意之前的帖子中提到的最简单的解决方法是通过在这些包中添加一个无操作的非测试文件来避免仅测试的包。例如:echo "package foo" >foo/foo.go。当上述修复方法在稳定版本中发布时,可以撤销这个解决方法。

英文:

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.

答案2

得分: 0

我强烈推荐使用go-acc工具。这是我们进行准确的跨包覆盖率测量的方法。

所以基本上你只需要这样做:

$ goacc ./... -o=coverage.out
英文:

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

huangapple
  • 本文由 发表于 2021年11月8日 14:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69879443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定