如何在Go中一起获取所有包的代码覆盖率?

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

How to get all packages' code coverage together in Go?

问题

我有一个包含多个包的库。在运行测试时,我使用了'-cover'标志,并且它显示了每个包的覆盖率信息。如下所示:

--- PASS: TestSampleTestSuite (0.00s)
PASS
coverage: 28.7% of statements
ok  	github.com/path/to/package1	13.021s
?   	github.com/path/to/package2	[no test files]

=== RUN   TestAbc
--- PASS: TestAbc (0.43s)
PASS
coverage: 27.7% of statements

有没有办法轻松地获取完整的覆盖率概述,以便对整个项目的覆盖率有一个好的了解?

更新:这是我正在使用的go test命令

go test ./... -v -short -p 1 -cover
英文:

I have a library consisting of several packages. When running tests, I am using '-cover' flag and its showing the coverage information for each package individually.Like follows:

--- PASS: TestSampleTestSuite (0.00s)
PASS
coverage: 28.7% of statements
ok  	github.com/path/to/package1	13.021s
?   	github.com/path/to/package2	[no test files]

=== RUN   TestAbc
--- PASS: TestAbc (0.43s)
PASS
coverage: 27.7% of statements

Is there any way to get a full coverage overview easily to get good idea about coverage on the whole project?

Update: Here is the go test command I am using

go test ./... -v -short -p 1 -cover

答案1

得分: 53

编辑:自从我写下这个答案以来,情况发生了变化。请参阅Go 1.10的发布说明:https://golang.org/doc/go1.10#test :

> go test -coverpkg标志现在将其参数解释为逗号分隔的模式列表,用于匹配每个测试的依赖项,而不是要重新加载的包列表。例如,现在可以使用go test -coverpkg=all来在测试包及其所有依赖项上启用覆盖率。此外,当运行多个测试时,现在支持go test -coverprofile选项。

现在可以运行以下命令:

go test -v -coverpkg=./... -coverprofile=profile.cov ./...
go tool cover -func profile.cov

旧答案

这是从https://github.com/h12w/gosweep提取的一个bash脚本:

#!/bin/bash
set -e

echo 'mode: count' > profile.cov

for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
do
if ls $dir/*.go &> /dev/null; then
    go test -short -covermode=count -coverprofile=$dir/profile.tmp $dir
    if [ -f $dir/profile.tmp ]
    then
        cat $dir/profile.tmp | tail -n +2 >> profile.cov
        rm $dir/profile.tmp
    fi
fi
done

go tool cover -func profile.cov
英文:

EDIT: Things have changed since I wrote this answer. See the release notes of Go 1.10: https://golang.org/doc/go1.10#test :

> The go test -coverpkg flag now interprets its argument as a
> comma-separated list of patterns to match against the dependencies of
> each test, not as a list of packages to load anew. For example, go
> test -coverpkg=all is now a meaningful way to run a test with coverage
> enabled for the test package and all its dependencies. Also, the go
> test -coverprofile option is now supported when running multiple
> tests.

You can now run

go test -v -coverpkg=./... -coverprofile=profile.cov ./...
go tool cover -func profile.cov

Old answer

Here is a bash script extracted from https://github.com/h12w/gosweep :

<!-- language: lang-bash -->

#!/bin/bash
set -e

echo &#39;mode: count&#39; &gt; profile.cov

for dir in $(find . -maxdepth 10 -not -path &#39;./.git*&#39; -not -path &#39;*/_*&#39; -type d);
do
if ls $dir/*.go &amp;&gt; /dev/null; then
    go test -short -covermode=count -coverprofile=$dir/profile.tmp $dir
    if [ -f $dir/profile.tmp ]
    then
        cat $dir/profile.tmp | tail -n +2 &gt;&gt; profile.cov
        rm $dir/profile.tmp
    fi
fi
done

go tool cover -func profile.cov

huangapple
  • 本文由 发表于 2015年10月31日 05:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/33444968.html
匿名

发表评论

匿名网友

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

确定