Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang coverprofile output format

Tags:

testing

go

cover

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?
like image 769
Christian Nuss Avatar asked Jul 14 '15 17:07

Christian Nuss


3 Answers

The fields are:

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

Source

like image 67
mislav Avatar answered Oct 26 '22 01:10

mislav


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

like image 15
Christian Nuss Avatar answered Oct 26 '22 00:10

Christian Nuss


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
like image 10
JimB Avatar answered Oct 26 '22 00:10

JimB