有人成功地为 Go 单元测试生成代码覆盖率吗?我在网上找不到相应的工具。
有人成功地为 Go 单元测试生成代码覆盖率吗?我在网上找不到相应的工具。
go test
的一个主要新功能是,它现在可以计算并显示测试覆盖率结果,而且还可以借助一个新的、单独安装的 "go tool cover
" 程序来实现。
cover
工具是go.tools
子存储库 的一部分。可以通过运行以下命令进行安装:$ go get golang.org/x/tools/cmd/cover
cover 工具有两个作用:
首先,当给 "
go test
" 带上-cover
标志时,它会自动运行以重写包的源代码并插入插桩语句。然后像往常一样编译和运行测试,并报告基本的覆盖率统计信息:$ go test -coverprofile fmtcoverage.html fmt ok fmt 0.060s coverage: 91.4% of statements $
其次,为了得到更详细的报告,不同的 "go test" 标志可以创建一个覆盖率文件,然后使用 "
go tool cover
" 调用 cover 程序进行分析。
The latest versions of Go (2013/09/19) use:
go test -coverprofile <filename> <package name>
Details on how to generate and analyze coverage statistics can be found by running the commands
$ go help testflag $ go tool cover -help
go test -coverprofile cover.out
and then
go tool cover -html=cover.out
openscover.out
in your default browserI don't even want to wait for the browser to open, so I defined this alias:
alias gc=grep -v -e " 1$" cover.out
That I just type
gc
, and have a list of all the lines not yet covered (here: with acoverage.out
line not ending with "1
").
更新于2022年,可能适用于Go 1.19
提案:将Go的代码覆盖测试扩展到应用程序
虽然现有的基于"
go test
"的覆盖率工作流将继续得到支持,但该提案旨在将覆盖率作为"go build
"的新构建模式添加进来。就像用户可以使用"
go build -race
"构建一个带有竞态检测器的可执行文件一样,使用"go build -cover
"将可以构建一个覆盖率检测器的可执行文件。支持合并在不同GOOS/GOARCH环境中生成的覆盖率文件。
go test -coverprofile <filename> <package name>
。 - Frank Shearargo test -coverprofile cover.out
,然后执行 go tool cover -html=cover.out -o cover.html
,最后在浏览器中打开 cover.html
。 - Ivan Blackgo tool cover -html=cover.out
会自动打开浏览器,但在我的系统上无法正常工作。我更喜欢保持浏览器打开,并在需要时刷新页面。 - Ivan BlackGo语言提供了非常好用的测试和覆盖率工具。虽然所有的Go工具都有很好的文档,go tool cover -help
,但我建议阅读官方Go博客上的覆盖率文章。这篇文章有大量的示例,我强烈推荐!
我在我的~/.bash_profile中有这个函数(你可以将其粘贴到终端中尝试)。
cover () {
t="/tmp/go-cover.$$.tmp"
go test -coverprofile=$t $@ && go tool cover -html=$t && unlink $t
}
ok fmt 0.031s coverage: 91.9% of statements
此外,在您的浏览器中,您将看到该工具以红色显示所有未覆盖测试的代码行:
还可以仅保存HTML覆盖文件而不是在浏览器中打开它。这在测试和覆盖率由Jenkins等CI工具运行的情况下非常有用。这样,您可以从中央服务器提供覆盖文件,并且整个团队将能够查看每个构建的覆盖结果。
cat cover.out.html | grep '<option value="file' | sed -E 's/.*>(.*) \((.*)%\)<.*/\2 \1/' | sort -rn
- Mattias Wadman除了上面的好答案之外,我认为以下这三行代码是获得(包括所有软件包)最简单的方法:
go test -v -coverprofile cover.out ./YOUR_CODE_FOLDER/...
go tool cover -html cover.out -o cover.html
open cover.html
注意,在HTML文件中,您将找到一个下拉按钮,该按钮将引导您到所有文件。open cover.html
works fine but it generates unwanted listener logs in the same terminal in ubuntu as follows bash Gtk-Message: 00:03:41.903: Failed to load module "canberra-gtk-module" Gtk-Message: 00:03:41.904: Failed to load module "canberra-gtk-module"
- Fayaz...
用于递归搜索内部文件夹。 - undefined只需运行
go test -cover
或者go test -cover ./...
或者go test -coverprofile=coverage.out ./... ; go tool cover -func=coverage.out
或者查看源代码
go test -coverprofile=coverage.out ./... ; go tool cover -html=coverage.out
我在网上找不到这样的工具。
实际上...现在(2022年)有一个来自Nikolay Dubina的项目go-cover-treemap-web
上有这样的工具:
它不需要上传任何内容(处理仍然在本地进行),但是通过拖放您的覆盖文件,Web UI(使用Go WASM)将运行go-cover-treemap
并显示:
(gocovergage for https://github.com/gohugoio/hugo)
测试覆盖率报告:
a) 运行所有的测试并启用覆盖率 → go test ./... -coverprofile coverage.out
b) 获取单个函数以及总体覆盖率 → go tool cover -func coverage.out
c) 查看测试覆盖的代码行和未覆盖的代码行 → go tool cover -html=coverage.out -o coverage.html
。在浏览器中打开生成的 coverage.html
文件,分析详细的覆盖率信息。
如果您想直接在终端中查看函数的未覆盖行,我为此目的重新编写了覆盖工具。它可在https://github.com/gregoryv/uncover上获得。
用法
go get -u github.com/gregoryv/uncover/...
go test -coverprofile /tmp/c.out
uncover /tmp/c.out
屏幕截图
$ go tool
6a
6c
6g
6l
addr2line
api
cgo
cov
dist
ebnflint
fix
gotype
nm
objdump
pack
pprof
prof
vet
yacc
$ go tool cov -h
usage: cov [-lsv] [-g substring] [-m minlines] [6.out args...]
-g specifies pattern of interesting functions or files
go tool cov: exit status 1
$
我没有使用过它,这就是我所知道的全部。