gpt4 book ai didi

Go Coverage over multiple package 和 Gitlab Coverage Badge

转载 作者:行者123 更新时间:2023-12-01 22:14:35 26 4
gpt4 key购买 nike

我正在尝试为我的 gitlab 项目显示准确的覆盖率徽章。

问题是我有几个包,在 gitlab-ci.yml,我运行

go test $(go list ./... | grep -v /vendor/) -v -coverprofile .testCoverage.txt

我的输出是这样的:

$ go test -coverprofile=coverage.txt -covermode=atomic ./...
ok gitlab.com/[MASKED]/pam 10.333s coverage: 17.2% of statements
ok gitlab.com/[MASKED]/pam/acquisition 0.004s coverage: 57.7% of statements
ok gitlab.com/[MASKED]/pam/acquisition/api 0.005s coverage: 72.1% of statements
ok gitlab.com/[MASKED]/pam/acquisition/ftp 24.936s coverage: 73.1% of statements
ok gitlab.com/[MASKED]/pam/repartition 0.004s coverage: 90.1% of statements

我在 Gitlab 中的测试覆盖率解析正则表达式是:

^coverage:\s(\d+(?:\.\d+)?%)

如果我检查 .testCoverage,我会得到很多这样的行:

 gitlab.com/[MASKED]/pam/repartition/repartition.go:54.33,56.5 1 1

所以,当它只是最后一个包的覆盖率时,它给了我一个 90.1% 的结果。

我该怎么做?

最佳答案

根据this answer ,

我只需要另一个命令:

go tool cover -func profile.cov

这会给你结果:

✗ go tool cover -func profile.cov

gitlab.com/[MASKED]/pam/acquisition/acquisition.go:17: FetchAll 0.0%
gitlab.com/[MASKED]/pam/acquisition/acquisition.go:32: TransformData 100.0%
gitlab.com/[MASKED]/pam/acquisition/acquisition_mocks.go:13: FetchMeters 0.0%
gitlab.com/[MASKED]/pam/repartition/repartition.go:102: GroupMetersByOperation 100.0%
gitlab.com/[MASKED]/pam/repartition/repartition.go:111: SetProrataRedistributed 71.4%
total: (statements) 68.7%

在 gitlab 中,你可以更改正则表达式:

^coverage:\s(\d+(?:\.\d+)?%)

\(statements\)(?:\s+)?(\d+(?:\.\d+)?%)

现在,如果您有模拟,覆盖范围将包括它们,因此您必须在此 answer 之后删除它们:

go test . -coverprofile profile.cov.tmp
cat profile.cov.tmp | grep -v "_mocks.go" > cover.out
tool cover -func profile.cov

当然,你所有的模拟都应该放在后缀为:_mocks.go

的文件中

它应该可以工作。

希望对他人有所帮助!

关于Go Coverage over multiple package 和 Gitlab Coverage Badge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61246686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com