Golang的coverprofile输出格式是什么?

huangapple go评论78阅读模式
英文:

Golang coverprofile output format

问题

-coverprofile cover.out选项是用于go test命令的,我将为你解释一下这个选项的含义以及文件的格式。

例如,对于server.go进行覆盖测试,输出结果将保存在cover.out文件中:

mode: set
github.com/cnuss/api_server/server.go:47.2,48.16 2 0
github.com/cnuss/api_server/server.go:52.2,53.16 2 0
github.com/cnuss/api_server/server.go:57.2,58.16 2 0
github.com/cnuss/api_server/server.go:62.2,63.16 2 0
github.com/cnuss/api_server/server.go:67.2,68.16 2 0
github.com/cnuss/api_server/server.go:72.2,73.16 2 0
github.com/cnuss/api_server/server.go:77.2,78.16 2 0
  1. 不同列的含义是什么?

    • 第一列(mode)表示覆盖测试的模式,这里是"set"。
    • 第二列(文件路径)表示被测试的文件路径。
    • 第三列(覆盖范围)表示被测试代码的行号范围。
    • 第四列(语句计数)表示被执行的语句次数。
    • 第五列(未执行计数)表示未执行的语句次数。
  2. 输出格式是否符合"标准"格式,例如gcov、xunit等,并且是否可以转换为其他格式?
    输出格式不是标准格式,它是Go语言特定的格式。然而,你可以使用一些工具将其转换为其他格式,例如使用go tool cover命令将其转换为HTML格式的报告。

英文:

I'm trying to make sense of the -coverprofile cover.out option in go test, specifically the format of the file.

Covering server.go for example, yields the output in cover.out:

mode: set
github.com/cnuss/api_server/server.go:47.2,48.16 2 0
github.com/cnuss/api_server/server.go:52.2,53.16 2 0
github.com/cnuss/api_server/server.go:57.2,58.16 2 0
github.com/cnuss/api_server/server.go:62.2,63.16 2 0
github.com/cnuss/api_server/server.go:67.2,68.16 2 0
github.com/cnuss/api_server/server.go:72.2,73.16 2 0
github.com/cnuss/api_server/server.go:77.2,78.16 2 0
  1. What do each of the different columns mean?
  2. Is the format of the output in a "standard" format, e.g. gcov, xunit, etc. and convertable to another format?

答案1

得分: 58

这些字段是:

name.go:line.column,line.column numberOfStatements count

来源

英文:

The fields are:

name.go:line.column,line.column numberOfStatements count

Source

答案2

得分: 14

golang-nuts社区(https://groups.google.com/forum/#!forum/golang-nuts)提供了一些有用的工具,可以将Go代码覆盖率转换为更有用的格式。

JUnit格式(用于总结测试执行):

# 先决条件
go install github.com/jstemmer/go-junit-report/v2@latest
# 运行测试
go test -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml

Cobertura格式(用于详细的代码覆盖率):

# 先决条件
go get github.com/axw/gocov/gocov
go get github.com/AlekSi/gocov-xml
# 生成覆盖率文件
go test -coverprofile=cover.out
gocov convert cover.out | gocov-xml > coverage.xml

指引我找到这些工具的帖子在这里:
https://groups.google.com/forum/#!topic/golang-nuts/iUc68Zrxk_c

英文:

The golang-nuts community (https://groups.google.com/forum/#!forum/golang-nuts) provided a couple useful tools for converting Go coverage into more useful formats.

JUnit Format (for summarizing test executions):

# Prerequisites
go install github.com/jstemmer/go-junit-report/v2@latest
# Tests
go test -v 2>&1 ./... | go-junit-report -set-exit-code > report.xml

Cobertura Format (for detailing code coverage):

# Prerequisites
go get github.com/axw/gocov/gocov
go get github.com/AlekSi/gocov-xml
# Coverage
go test -coverprofile=cover.out
gocov convert cover.out | gocov-xml > coverage.xml

The thread that pointed me in this direction was here:
https://groups.google.com/forum/#!topic/golang-nuts/iUc68Zrxk_c

答案3

得分: 11

你可以使用go的cover工具处理覆盖率文件:

在浏览器中显示带注释的源代码:

go tool cover -html=c.out

将结果写入HTML文件而不是在浏览器中显示:

go tool cover -html=c.out -o coverage.html

在标准输出中显示每个函数的覆盖率百分比:

go tool cover -func=c.out
英文:

You process the cover profile using the go cover tool:

Open a web browser displaying annotated source code:

	go tool cover -html=c.out

Write out an HTML file instead of launching a web browser:

	go tool cover -html=c.out -o coverage.html

Display coverage percentages to stdout for each function:

	go tool cover -func=c.out

huangapple
  • 本文由 发表于 2015年7月15日 01:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/31413281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定