如何正确地覆盖所有的Golang包和所有的测试?

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

How to correctly cover all golang packages with all tests?

问题

假设有一些代码定义在pkg/somepkg1包下面。
在这个包中有一些测试,而且还有一些测试在tests/integration包中。

如果我运行以下命令:

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

我会运行所有的测试(包括integration),但是cover.out中没有覆盖率信息,只有somepkg1包的覆盖率!例如,78%。

如果我运行以下命令:

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

我得到了我想要的结果 - 我真正看到了代码somepackage1的所有覆盖率,无论它是来自它所在的包还是integration。例如,85%。

所以,如果我想要检查./pkg中的所有包,我对每个包都调用相同的代码,然后将答案合并到一个批处理中。这需要很长时间,对于持续集成来说也不好。

这对我来说一点也不一致。我知道相对于其他模块和集成测试来说,单元测试更好,但是这样使用起来并不友好。

有没有一种方法可以在不对每个包都调用这样的代码片段的情况下覆盖所有内容?

英文:

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?

答案1

得分: 1

使用以下命令来无差别地测试所有包:

 go test ./...

对于所有包,你可以添加覆盖率测试:

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

要在特定包内进行测试,可以使用以下命令:

go test --cover -coverpkg=./pkg ./pkg -coverprofile=cover.out
英文:

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

答案2

得分: 0

解决了!

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

在这种情况下,它会完全按照我想要的方式执行,与以下命令不同:

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

如果你想要测试一组包,可以使用以下命令:

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

使用逗号而不是空格。

英文:

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

huangapple
  • 本文由 发表于 2023年7月4日 12:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76609442.html
匿名

发表评论

匿名网友

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

确定