登录
首页 >  Golang >  Go问答

gocov 未能成功上传完整的测试覆盖率结果至 codeclimate

来源:stackoverflow

时间:2024-02-22 20:48:22 138浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《gocov 未能成功上传完整的测试覆盖率结果至 codeclimate》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

基于 stackoverflow 上的这个问题,我将测试覆盖率添加到我的 gitlab ci/cd yaml 文件中,我希望结果会上传到 codeclimate 中。

- go get github.com/axw/gocov/gocov
- export CC_TEST_REPORTER_ID=My_REPO_ID
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
- gocov test -v ./... -coverprofile=out
- ./cc-test-reporter format-coverage --input-type gocov out
- ./cc-test-reporter upload-coverage

该脚本在我的所有包中成功运行测试,ci/cd 的输出显示不同包中的所有测试都已运行,但上传到 codeclimate 的报告仅显示只有一个具有测试覆盖率的文件,并且这是最后一个文件测试包,而不是显示全部。


解决方案


我将解决方案与同一 stackoverflow 链接中的另一个答案混合在一起,最后创建一个 bash 文件并从我的 yaml 文件中运行它,如下所示,以便报告输出包含所有包报告,这是我的脚本:

go get github.com/axw/gocov/gocov
export CC_TEST_REPORTER_ID=My_ID
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build

for pkg in $(go list ./... | grep -v vendor); do
    go test -coverprofile=$(echo $pkg | tr / -).cover $pkg
done
echo "mode: set" > c.out
grep -h -v "^mode:" ./*.cover >> c.out
rm -f *.cover

./cc-test-reporter after-build

本篇关于《gocov 未能成功上传完整的测试覆盖率结果至 codeclimate》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>