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
The fields are:
name.go:line.column,line.column numberOfStatements count
Source
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 get github.com/jstemmer/go-junit-report
# Tests
go test -v | go-junit-report > 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With