gpt4 book ai didi

unit-testing - 如何在 Go 中将所有包的代码覆盖率放在一起?

转载 作者:行者123 更新时间:2023-11-28 21:35:23 24 4
gpt4 key购买 nike

我有一个包含多个包的库。运行测试时,我使用“-cover”标志并单独显示每个包的覆盖率信息。如下所示:

--- 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

有没有什么方法可以轻松获得完整的覆盖率概览,从而更好地了解整个项目的覆盖率?

更新:这是我正在使用的 go test 命令

go test ./... -v -short -p 1 -cover

最佳答案

编辑:自从我写下这个答案以来,情况发生了变化。请参阅 Go 1.10 的发行说明:https://golang.org/doc/go1.10#test :

The go test -coverpkg flag now interprets its argument as a comma-separated list of patterns to match against the dependencies of each test, not as a list of packages to load anew. For example, go test -coverpkg=all is now a meaningful way to run a test with coverage enabled for the test package and all its dependencies. Also, the go test -coverprofile option is now supported when running multiple tests.

你现在可以运行了

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

旧答案

这是从 https://github.com/h12w/gosweep 中提取的 bash 脚本:

#!/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

关于unit-testing - 如何在 Go 中将所有包的代码覆盖率放在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59354924/

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