DevOps Go多个pkg的单元测试覆盖率

erlv · November 14, 2019 · 74 hits

在 go test 命令后面添加 -cover 参数开启测试覆盖率统计,其结果如下:
ok models 0.012s coverage: 71.4% of statements in models
-coverpkg 标记来指定要被统计的代码包之后,未被指定的代码则肯定不会被统计,即使是被直接测试的那个代码包。go test -coverpkg=./... pkg2可以跑 pkg2 下的所有单元测试及 pkg2 所用到的其他包的覆盖率情况。
但是由于 go 不支持go test -coverpkg=./... ./...如果我们有多个 pkg,则无法一次性统计出所有的测试覆盖率和跑完全部单元测试。所以需要挨个跑完单元测试然自己来合并覆盖率的结果:

1
2
3
4
5
6
7
8
9
10
src
├── pkg1
│ ├── pkg11
│ └── pkg12
└── pkg2
├── pkg21
└── pkg22

go test -coverprofile=pkg1.cover.out -coverpkg=./... pkg1
go test -coverprofile=pkg1.cover.out -coverpkg=./... pkg2

-coverprofile 用来指定统计测试覆盖率信息的输出路径,其内容如下:

1
2
3
4
mode: set
models/bot.go:32.40,46.18 12 1
models/bot.go:49.2,57.35 5 1
...

第一行是测试覆盖的 mode,可取值为:set,count,atomic。剩下的行遵循以下的格式:
name.go:line.column,line.column numberOfStatements count
所以对每个 pkg 跑完单元测试后可以用以下的命令来合并:

1
2
echo "mode: set" > coverage.out && cat *.cover.out | grep -v mode: | sort -r | 
awk '{if($1 != last) {print $0;last=$1}}' >> coverage.out

然后用 go 自带的工具来将其输出为 HTML:
go tool cover -html=coverage.out -o cover.html
把整个流程串起来写成脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash

set -e

profile="cover.out"
htmlfile="cover.html"
mergecover="merge_cover"
mode="set"

for package in $(go list ./...|grep -v src); do
coverfile="$(echo $package | tr / -).cover"
go test -covermode="$mode" -coverprofile="$coverfile" -coverpkg="$package" "$package"
done

# merge all profiles
grep -h -v "^mode:" *.cover | sort > $mergecover

# aggregate duplicated code-block data
echo "mode: $mode" > $profile
current=""
count=0
while read line; do
block=$(echo $line | cut -d ' ' -f1-2)
num=$(echo $line | cut -d ' ' -f3)
if [ "$current" == "" ]; then
current=$block
count=$num
elif [ "$block" == "$current" ]; then
count=$(($count + $num))
else
echo $current $count >> $profile
current=$block
count=$num
fi
done < $mergecover

if [ "$current" != "" ]; then
echo $current $count >> $profile
fi

# save result
go tool cover -html=$profile -o $htmlfile
go tool cover -func=$profile
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.