本文介绍了如何在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的发行说明::

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 -v -coverpkg=./... -coverprofile=profile.cov ./...
go tool cover -func profile.cov






旧答案

这里是从:

#!/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中将所有程序包的代码覆盖范围汇总在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:50