如何计算测试的总代码覆盖率

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

How to calculate total code coverage for tests

问题

我需要计算一个使用Java语言编写的集成测试作为测试源的golang项目的代码覆盖率。这需要先对go build进行插桩,然后在服务器上运行,以便运行测试并在测试结束后得知代码覆盖率。我在互联网上没有找到相关的参考资料,只有可以轻松运行并用于计算覆盖率的单元测试。

英文:

I need to calculate code coverage for golang project where source of tests will be integration tests written in Java language . This requires go build to be instrumented first and then run on server so that tests can run and we will get to know after tests have ended, how much is code coverage? I haven't found a single reference for this on internet all there is present is unit tests which can be run easily and used to calculate coverage

答案1

得分: 1

首先,使用-coverprofile标志生成覆盖率文件,并告诉命令在哪里存储信息。

然后,使用go tool cover命令和-func标志,指向之前生成的文件。这将显示项目中每个包的代码覆盖率,并在底部显示总覆盖率。

最后,使用管道和其他命令来提取所需的覆盖率信息。

请注意,这些命令是针对Go语言的,需要在命令行中执行。

英文:

First of all generate the coverage profile by using the -coverprofile flag and tell the command where to store the information.

%  go test ./... -coverprofile cover.out              
?       some.project/somepkg            [no test files]
ok      some.project/somepkg/service    0.329s  coverage: 53.3% of statements

Than use go tool cover with the -func flag and point it to the previously generated file. This will show you the code coverage for every single package withing your project and down at the bottom the total coverage.

%  go tool cover -func cover.out         

some.project/somepkg/service/.go:27:          FuncA        100.0%
some.project/somepkg/service/service.go:33:   FuncB        100.0%
some.project/somepkg/service/service.go:51:   FuncC        0.0%
total:                                        (statements) 53.3%

%  go tool cover -func cover.out | fgrep total:
total:                                        (statements) 53.3%
%  go tool cover -func cover.out | fgrep total | awk '{print $3}'
100.0%
%  go tool cover -func cover.out | fgrep total | awk '{print substr($3, 1, length($3)-1)}'
100.0

答案2

得分: -1

在Go 1.20之前,你无法做到这一点。只支持对单元测试进行覆盖率分析。

然而,从Go 1.20开始,现在可以实现。

https://go.dev/testing/coverage/

> 从Go 1.20开始,Go支持从应用程序和集成测试中收集覆盖率分析数据,这些是针对Go程序的更大型、更复杂的测试。

https://go.dev/doc/go1.20

> 要收集程序的覆盖率数据,使用go build命令的-cover标志进行构建,然后在运行生成的二进制文件时,将环境变量GOCOVERDIR设置为覆盖率分析数据的输出目录。有关如何入门的更多信息,请参阅“集成测试的覆盖率分析”页面。有关设计和实现的详细信息,请参阅提案。

英文:

Before go 1.20 you could not do it. Only coverage for unit tests was supported.

However with go 1.20 it is now possible

https://go.dev/testing/coverage/:

> Beginning in Go 1.20, Go supports collection of coverage profiles from applications and from integration tests, larger and more complex tests for Go programs.

https://go.dev/doc/go1.20:

> To collect coverage data for a program, build it with go build's -cover flag, then run the resulting binary with the environment variable GOCOVERDIR set to an output directory for coverage profiles. See the 'coverage for integration tests' landing page for more on how to get started. For details on the design and implementation, see the proposal.

huangapple
  • 本文由 发表于 2022年11月14日 11:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/74426546.html
匿名

发表评论

匿名网友

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

确定