英文:
Is it possible to post coverage for multiple packages to Coveralls?
问题
我想使用Coveralls来跟踪Go项目的测试覆盖率,集成的说明中提到使用https://github.com/mattn/goveralls
cd $GOPATH/src/github.com/yourusername/yourpackage
$ goveralls your_repos_coveralls_token
然而,这只会发布一个包的结果,依次运行多个包的方式不起作用,因为最后一次运行会覆盖之前的所有运行结果。有人找到了如何获取多个包的覆盖率的方法吗?
英文:
I want to track test coverage on a go project using Coveralls, the instructions for the integration reference using
https://github.com/mattn/goveralls
cd $GOPATH/src/github.com/yourusername/yourpackage
$ goveralls your_repos_coveralls_token
However, this only posts the results for one package and running for packages in turn does not work as the final run overwrites all other runs. Has anyone figured out how to get coverage for multiple packages?
答案1
得分: 10
我最终使用了这个脚本:
<!-- language: lang-bash -->
echo "mode: set" > acc.out
for Dir in $(find ./* -maxdepth 10 -type d );
do
if ls $Dir/*.go &> /dev/null;
then
go test -coverprofile=profile.out $Dir
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
fi
done
goveralls -coverprofile=acc.out $COVERALLS
rm -rf ./profile.out
rm -rf ./acc.out
它基本上会在路径中找到所有的目录,并分别为它们打印一个覆盖率概要。然后将这些文件连接成一个大的概要文件,并将其发送到coveralls。
英文:
I ended up using this script:
<!-- language: lang-bash -->
echo "mode: set" > acc.out
for Dir in $(find ./* -maxdepth 10 -type d );
do
if ls $Dir/*.go &> /dev/null;
then
go test -coverprofile=profile.out $Dir
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
fi
done
goveralls -coverprofile=acc.out $COVERALLS
rm -rf ./profile.out
rm -rf ./acc.out
It basically finds all the directories in the path and prints a coverage profile for them separately. It then concatenates the files into one big profile and ships them off to coveralls.
答案2
得分: 5
根据Usman的答案,我对其进行了修改,以支持跳过Godep和其他无关的文件夹:
echo "mode: set" > acc.out
for Dir in $(go list ./...);
do
returnval=`go test -coverprofile=profile.out $Dir`
echo ${returnval}
if [[ ${returnval} != *FAIL* ]]
then
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
else
exit 1
fi
done
if [ -n "$COVERALLS_TOKEN" ]
then
goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi
rm -rf ./profile.out
rm -rf ./acc.out
请注意,我使用了go list ./...
命令,而不是查看每个目录,该命令列出了实际用于构建Go包的所有目录。
希望对其他人有所帮助。
编辑
如果您正在使用Go v.1.6+的vendor
文件夹,则此脚本会过滤掉依赖项:
echo "mode: set" > acc.out
for Dir in $(go list ./...);
do
if [[ ${Dir} != */vendor/* ]]
then
returnval=`go test -coverprofile=profile.out $Dir`
echo ${returnval}
if [[ ${returnval} != *FAIL* ]]
then
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
else
exit 1
fi
else
exit 1
fi
done
if [ -n "$COVERALLS_TOKEN" ]
then
goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi
英文:
Taking Usman's answer, and altering it to support skipping Godep and other irrelevant folders:
<!-- language: lang-bash -->
echo "mode: set" > acc.out
for Dir in $(go list ./...);
do
returnval=`go test -coverprofile=profile.out $Dir`
echo ${returnval}
if [[ ${returnval} != *FAIL* ]]
then
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
else
exit 1
fi
done
if [ -n "$COVERALLS_TOKEN" ]
then
goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi
rm -rf ./profile.out
rm -rf ./acc.out
Notice that instead of looking at every directory, I us the go list ./...
command which lists all directories that actually get used to build the go package.
Hope that helps others.
** EDIT **
If you are using the vendor
folder for Go v.1.6+ then this script filters out the dependencies:
<!-- language: lang-bash -->
echo "mode: set" > acc.out
for Dir in $(go list ./...);
do
if [[ ${Dir} != *"/vendor/"* ]]
then
returnval=`go test -coverprofile=profile.out $Dir`
echo ${returnval}
if [[ ${returnval} != *FAIL* ]]
then
if [ -f profile.out ]
then
cat profile.out | grep -v "mode: set" >> acc.out
fi
else
exit 1
fi
else
exit 1
fi
done
if [ -n "$COVERALLS_TOKEN" ]
then
goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
fi
答案3
得分: 4
有人找到了如何为多个包获取覆盖率的方法吗?
注意:从Go 1.10(2018年第一季度)开始,这将成为可能。
参见CL 76875
cmd/go
:允许在测试多个包时使用-coverprofile
。
您可以在commit 283558e中查看多个包代码覆盖测试的实现。
自Go 1.10发布(2018年2月)以来,Jeff Martin在评论中确认:
go test -v -cover ./pkgA/... ./pkgB/... -coverprofile=cover.out
可以得到一个良好的覆盖率文件go tool cover -func "cover.out"
将显示total: (statements) 52.5%
。
所以它是有效的!
英文:
> Has anyone figured out how to get coverage for multiple packages?
Note: with Go 1.10 (Q1 2018), that... will actually be possible.
See CL 76875
> ## cmd/go
: allow -coverprofile
with multiple packages being tested
You can see the implementation of a multiple package code coverage test in commit 283558e
Jeff Martin has since the release of Go 1.10 (Feb. 2018) confirmed in the comments:
go test -v -cover ./pkgA/... ./pkgB/... -coverprofile=cover.out
gets a good profile andgo tool cover -func "cover.out"
will get atotal: (statements) 52.5%
.
So it is working!
答案4
得分: 2
我一直在使用http://github.com/axw/gocov来获取我的代码覆盖率。
我在一个bash脚本中触发这个操作,在这里我调用了所有的包。
我还使用http://github.com/matm/gocov-html将其格式化为html。
coverage)
echo "测试代码覆盖率"
cd "${SERVERPATH}/package1/pack"
GOPATH=${GOPATH} gocov test ./... > coverage.json
GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
cd "${SERVERPATH}/package2/pack"
GOPATH=${GOPATH} gocov test ./... > coverage.json
GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
;;
希望这能有所帮助。
英文:
I have been using http://github.com/axw/gocov to get my code coverage.
I trigger this in a bash script, in here I call all my packages.
I also use http://github.com/matm/gocov-html to format into html.
coverage)
echo "Testing Code Coverage"
cd "${SERVERPATH}/package1/pack"
GOPATH=${GOPATH} gocov test ./... > coverage.json
GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
cd "${SERVERPATH}/package2/pack"
GOPATH=${GOPATH} gocov test ./... > coverage.json
GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
;;
Hope that helps a little bit.
答案5
得分: 1
这是一个纯Go语言的解决方案:
我创建了一个可能有帮助的库,https://github.com/bluesuncorp/overalls
它的功能是递归地遍历每个目录(也就是每个包),运行go test并生成覆盖率文件,然后将所有的覆盖率文件合并成一个单独的文件,放在项目根目录下,命名为overalls.coverprofile。
然后你可以使用像https://github.com/mattn/goveralls这样的工具将其发送到coveralls.io。
希望大家喜欢。
英文:
Here is a pure GO solution:
I create a library that may help, https://github.com/bluesuncorp/overalls
all it does is recursively go through each directory ( aka each package ), run go test and produce coverprofiles, then merges all profiles into a single one at the root of the project directory called overalls.coverprofile
then you can use a tool like https://github.com/mattn/goveralls to send it to coveralls.io
hope everyone likes
答案6
得分: 1
在Go 1.13中,以下命令可以为多个包生成覆盖率报告:
go test -v -coverpkg=./... -coverprofile=profile.cov ./...
go tool cover -func profile.cov
生成HTML报告的命令如下:
go tool cover -html=profile.cov -o cover.html
英文:
In Go 1.13, following command generates coverage for multiple packages
go test -v -coverpkg=./... -coverprofile=profile.cov ./...
go tool cover -func profile.cov
for html report
go tool cover -html=profile.cov -o cover.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论