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

。_饼干妹妹 提交于 2020-01-10 17:59:43

问题


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:


Here is a bash script extracted from 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


来源:https://stackoverflow.com/questions/33444968/how-to-get-all-packages-code-coverage-together-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!