如何在所有包中实现完全覆盖的测试(Golang)

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

How to get a complete coverage in all package golang

问题

我刚刚开始使用Go,对此还非常陌生。

我正在尝试进行完整的测试转换,但是当我运行以下命令时:

go test -cover new-project/anto/... .

new-project/anto/mon 目录中,它显示我的覆盖率为51.8%,但实际上我的覆盖率更高,因为在 new-project/anto/integration 目录中有一些测试是在导入 new-project/anto/mon 进行测试的。

问题在于,根据我阅读的Golang文档,只有当测试文件与被测试的代码文件在同一个文件夹中时,Go才会正确地检测到它们。

我想知道是否有一种方法可以获取完整的覆盖率信息。

go test -cover new-project/anto/... .
?   	new-project/anto/		[没有测试文件]
ok  	new-project/anto/conf	6.060秒	覆盖率:73.1%
ok  	new-project/anto/integration	43.309秒	覆盖率:[没有语句]
?   	new-project/anto/integration/conf	[没有测试文件]
?   	new-project/anto/integration/mocks	[没有测试文件]
?   	new-project/anto/integration/utils	[没有测试文件]
ok  	new-project/anto/logs	0.024秒	覆盖率:70.5%
ok  	new-project/anto/metric	0.012秒	覆盖率:59.0%
ok  	new-project/anto/mon	1.612秒	覆盖率:51.8%
?   	new-project/anto/anotherComponents	[没有测试文件]

经过一些研究,我找到了一个名为 go-acc 的插件,它可以验证所有的包。有趣的是,使用该插件后,mon 的覆盖率从51.8%降低到了29.8%。

go-acc new-project/anto/... .
?   	new-project/anto/	[没有测试文件]
ok  	new-project/anto//conf	5.030秒	覆盖率:73.1
ok  	new-project/anto//integration	43.117秒	覆盖率:73.5
?   	new-project/anto//integration/conf	[没有测试文件]
?   	new-project/anto//integration/mocks	[没有测试文件]
?   	new-project/anto//integration/utils	[没有测试文件]
ok  	new-project/anto//logs	0.008秒	覆盖率:22.3
ok  	new-project/anto//metric	0.020秒	覆盖率:59.0
ok  	new-project/anto//mon	1.460秒	覆盖率:29.8
?   	new-project/anto//anotherComponents  	[没有测试文件]
?   	new-project/anto/	[没有测试文件]
英文:

I have just 24h working with Go so I am super new to this.

I'm trying to get a complete test conversion but when I run

go test -cover new-project/anto/... .

in new-project/anto/mon it shows me that I have 51.8% coverage but in reality I have more coverage because there are tests that are tested in new-project/anto/integration where I have a test file that I import the new-project/anto/mon for testing.

The thing is that as I was reading Golang only takes the tests if they are in the same folder but having separate files does not detect it correctly.

I was wondering if there is a way to get the full coverage.

go test -cover new-project/anto/... .
?   	new-project/anto/		[no test files]
ok  	new-project/anto/conf	6.060s	coverage: 73.1% of statements
ok  	new-project/anto/integration	43.309s	coverage: [no statements]
?   	new-project/anto/integration/conf	[no test files]
?   	new-project/anto/integration/mocks	[no test files]
?   	new-project/anto/integration/utils	[no test files]
ok  	new-project/anto/logs	0.024s	coverage: 70.5% of statements
ok  	new-project/anto/metric	0.012s	coverage: 59.0% of statements
ok  	new-project/anto/mon	1.612s	coverage: 51.8% of statements
?   	new-project/anto/anotherComponents	[no test files]

Doing some research, I found a go-acc plugin that validates all the packages, the curious thing is that mon instead of having 51.8% now has 29.8%.

go-acc new-project/anto/... .
?   	new-project/anto/	[no test files]
ok  	new-project/anto//conf	5.030s	coverage: 73.1
ok  	new-project/anto//integration	43.117s	coverage: 73.5
?   	new-project/anto//integration/conf	[no test files]
?   	new-project/anto//integration/mocks	[no test files]
?   	new-project/anto//integration/utils	[no test files]
ok  	new-project/anto//logs	0.008s	coverage: 22.3
ok  	new-project/anto//metric	0.020s	coverage: 59.0
ok  	new-project/anto//mon	1.460s	coverage: 29.8
?   	new-project/anto//anotherComponents  	[no test files]
?   	new-project/anto/	[no test files]

答案1

得分: 11

第一条命令将为所有包生成coverage.out文件。第二条命令解析coverage.out并输出结果:

go test ./...  -coverpkg=./... -coverprofile ./coverage.out
go tool cover -func ./coverage.out

示例输出:

golang.org/x/example/stringutil/reverse.go:21: Reverse      100.0%
total:                                         (statements) 100.0%

要获取更多详细信息,请参阅以下各种帮助输出:

  • go help test
  • go help testflags
  • go help packages
  • go tool cover --help
英文:

The first command will generate coverage.out for all packages. The second parses the coverage.out and outputs the results:

go test ./...  -coverpkg=./... -coverprofile ./coverage.out
go tool cover -func ./coverage.out

Sample output:

golang.org/x/example/stringutil/reverse.go:21: Reverse      100.0%
total:                                         (statements) 100.0%

For more details see the various help outputs:

  • go help test
  • go help testflags
  • go help packages
  • go tool cover --help

huangapple
  • 本文由 发表于 2021年9月7日 17:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/69085529.html
匿名

发表评论

匿名网友

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

确定