可以将多个软件包的覆盖率发布到Coveralls吗?

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

Is it possible to post coverage for multiple packages to Coveralls?

问题

我想使用Coveralls来跟踪Go项目的测试覆盖率,集成的说明中提到使用https://github.com/mattn/goveralls

  1. cd $GOPATH/src/github.com/yourusername/yourpackage
  2. $ 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

  1. cd $GOPATH/src/github.com/yourusername/yourpackage
  2. $ 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 -->

  1. echo "mode: set" &gt; acc.out
  2. for Dir in $(find ./* -maxdepth 10 -type d );
  3. do
  4. if ls $Dir/*.go &amp;&gt; /dev/null;
  5. then
  6. go test -coverprofile=profile.out $Dir
  7. if [ -f profile.out ]
  8. then
  9. cat profile.out | grep -v "mode: set" &gt;&gt; acc.out
  10. fi
  11. fi
  12. done
  13. goveralls -coverprofile=acc.out $COVERALLS
  14. rm -rf ./profile.out
  15. rm -rf ./acc.out

它基本上会在路径中找到所有的目录,并分别为它们打印一个覆盖率概要。然后将这些文件连接成一个大的概要文件,并将其发送到coveralls。

英文:

I ended up using this script:

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

  1. echo &quot;mode: set&quot; &gt; acc.out
  2. for Dir in $(find ./* -maxdepth 10 -type d );
  3. do
  4. if ls $Dir/*.go &amp;&gt; /dev/null;
  5. then
  6. go test -coverprofile=profile.out $Dir
  7. if [ -f profile.out ]
  8. then
  9. cat profile.out | grep -v &quot;mode: set&quot; &gt;&gt; acc.out
  10. fi
  11. fi
  12. done
  13. goveralls -coverprofile=acc.out $COVERALLS
  14. rm -rf ./profile.out
  15. 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和其他无关的文件夹:

  1. echo "mode: set" > acc.out
  2. for Dir in $(go list ./...);
  3. do
  4. returnval=`go test -coverprofile=profile.out $Dir`
  5. echo ${returnval}
  6. if [[ ${returnval} != *FAIL* ]]
  7. then
  8. if [ -f profile.out ]
  9. then
  10. cat profile.out | grep -v "mode: set" >> acc.out
  11. fi
  12. else
  13. exit 1
  14. fi
  15. done
  16. if [ -n "$COVERALLS_TOKEN" ]
  17. then
  18. goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
  19. fi
  20. rm -rf ./profile.out
  21. rm -rf ./acc.out

请注意,我使用了go list ./...命令,而不是查看每个目录,该命令列出了实际用于构建Go包的所有目录。

希望对其他人有所帮助。

编辑

如果您正在使用Go v.1.6+的vendor文件夹,则此脚本会过滤掉依赖项:

  1. echo "mode: set" > acc.out
  2. for Dir in $(go list ./...);
  3. do
  4. if [[ ${Dir} != */vendor/* ]]
  5. then
  6. returnval=`go test -coverprofile=profile.out $Dir`
  7. echo ${returnval}
  8. if [[ ${returnval} != *FAIL* ]]
  9. then
  10. if [ -f profile.out ]
  11. then
  12. cat profile.out | grep -v "mode: set" >> acc.out
  13. fi
  14. else
  15. exit 1
  16. fi
  17. else
  18. exit 1
  19. fi
  20. done
  21. if [ -n "$COVERALLS_TOKEN" ]
  22. then
  23. goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
  24. fi
英文:

Taking Usman's answer, and altering it to support skipping Godep and other irrelevant folders:

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

  1. echo &quot;mode: set&quot; &gt; acc.out
  2. for Dir in $(go list ./...);
  3. do
  4. returnval=`go test -coverprofile=profile.out $Dir`
  5. echo ${returnval}
  6. if [[ ${returnval} != *FAIL* ]]
  7. then
  8. if [ -f profile.out ]
  9. then
  10. cat profile.out | grep -v &quot;mode: set&quot; &gt;&gt; acc.out
  11. fi
  12. else
  13. exit 1
  14. fi
  15. done
  16. if [ -n &quot;$COVERALLS_TOKEN&quot; ]
  17. then
  18. goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
  19. fi
  20. rm -rf ./profile.out
  21. 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 -->

  1. echo &quot;mode: set&quot; &gt; acc.out
  2. for Dir in $(go list ./...);
  3. do
  4. if [[ ${Dir} != *&quot;/vendor/&quot;* ]]
  5. then
  6. returnval=`go test -coverprofile=profile.out $Dir`
  7. echo ${returnval}
  8. if [[ ${returnval} != *FAIL* ]]
  9. then
  10. if [ -f profile.out ]
  11. then
  12. cat profile.out | grep -v &quot;mode: set&quot; &gt;&gt; acc.out
  13. fi
  14. else
  15. exit 1
  16. fi
  17. else
  18. exit 1
  19. fi
  20. done
  21. if [ -n &quot;$COVERALLS_TOKEN&quot; ]
  22. then
  23. goveralls -coverprofile=acc.out -repotoken=$COVERALLS_TOKEN -service=travis-pro
  24. 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 and
  • go tool cover -func &quot;cover.out&quot; will get a total: (statements) 52.5%.

So it is working!

答案4

得分: 2

我一直在使用http://github.com/axw/gocov来获取我的代码覆盖率。

我在一个bash脚本中触发这个操作,在这里我调用了所有的包。

我还使用http://github.com/matm/gocov-html将其格式化为html。

  1. coverage)
  2. echo "测试代码覆盖率"
  3. cd "${SERVERPATH}/package1/pack"
  4. GOPATH=${GOPATH} gocov test ./... > coverage.json
  5. GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
  6. cd "${SERVERPATH}/package2/pack"
  7. GOPATH=${GOPATH} gocov test ./... > coverage.json
  8. GOPATH=${GOPATH} gocov-html coverage.json > coverage_report.html
  9. ;;

希望这能有所帮助。

英文:

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.

  1. coverage)
  2. echo &quot;Testing Code Coverage&quot;
  3. cd &quot;${SERVERPATH}/package1/pack&quot;
  4. GOPATH=${GOPATH} gocov test ./... &gt; coverage.json
  5. GOPATH=${GOPATH} gocov-html coverage.json &gt; coverage_report.html
  6. cd &quot;${SERVERPATH}/package2/pack&quot;
  7. GOPATH=${GOPATH} gocov test ./... &gt; coverage.json
  8. GOPATH=${GOPATH} gocov-html coverage.json &gt; coverage_report.html
  9. ;;

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中,以下命令可以为多个包生成覆盖率报告:

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

生成HTML报告的命令如下:

  1. go tool cover -html=profile.cov -o cover.html
英文:

In Go 1.13, following command generates coverage for multiple packages

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

for html report

  1. go tool cover -html=profile.cov -o cover.html

huangapple
  • 本文由 发表于 2014年1月15日 07:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/21126011.html
匿名

发表评论

匿名网友

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

确定