如何在Go中将所有程序包的代码覆盖范围汇总在一起? [英] How to get all packages' code coverage together in Go?

查看:167
本文介绍了如何在Go中将所有程序包的代码覆盖范围汇总在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个程序包的库。运行测试时,我使用'-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?

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

Update: Here is the go test command I am using

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


推荐答案

编辑:自从我撰写此答案以来,事情已经发生了变化。请参阅Go 1.10的发行说明: https://golang.org/doc/go1.10# test

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


go test -coverpkg标志现在将其参数解释为
逗号分隔的模式列表以匹配每个测试的
的依赖关系,而不是作为要重新加载的软件包的列表。例如,go
test -coverpkg = all现在是一种运行测试的有意义的方法,该测试为测试包及其所有依赖项启用了coverage
。另外,运行多个
测试时现在支持go
test -coverprofile选项。

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.

您现在可以运行

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






旧答案

这里是从 https://github.com/h12w/gosweep

#!/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

这篇关于如何在Go中将所有程序包的代码覆盖范围汇总在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆