46

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?
030
  • 10,842
  • 12
  • 78
  • 123
Christian Nuss
  • 841
  • 2
  • 7
  • 13

3 Answers3

59

The fields are:

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

Source

mislav
  • 14,919
  • 8
  • 47
  • 63
  • 6
    From a recent research I found that the last field is actually a 0/1 flag that tells whether the specified statements are covered or not – Tareq Sha Jun 19 '19 at 10:05
  • Thank you, I finally found what I was looking for. – Roger Russel May 15 '20 at 03:46
  • Re: @Tareq Sha, I just had a look at go 1.15.2 and the last field had values that weren't 0/1, count seems more likely – Paul Rubel Dec 01 '20 at 20:40
  • 3
    @TareqSha it's the number of times the line was covered. If the report was generated with -covermode=set, the values will only be 0 and 1 as that mode doesn't keep counts. With -covermode=count or -covermode=atomic you can see actual counts. – FGM Dec 04 '21 at 07:53
  • @Christian Nuss this should be marked as the correct answer – Maria Ines Parnisari Oct 20 '22 at 22:46
14

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

jackotonye
  • 3,537
  • 23
  • 31
Christian Nuss
  • 841
  • 2
  • 7
  • 13
11

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
Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69
JimB
  • 104,193
  • 13
  • 262
  • 255
  • I'm not seeing ```-html``` or ```-func``` as an option... Am I missing something? http://pastebin.com/d2wgQyDM – Christian Nuss Jul 14 '15 at 18:25
  • 1
    @ChristianNuss: you need to use the cover tool. Use the commands exactly as they are printed in my answer (which is taken directly from `go tool cover -help`) – JimB Jul 14 '15 at 18:33
  • Ahh, thanks, First run go test -coverprofile, and then run go tool cover on that output file. Still, is there a way to convert the raw cover output file into another format, preferably xunit? – Christian Nuss Jul 14 '15 at 19:42
  • 2
    @ChristianNuss: I'm not aware of any translators. The mailing list would probably be the best place to ask if anyone has written one. – JimB Jul 14 '15 at 19:50
  • Useful comment from the forums: I need to try each out but the tools seem viable: https://groups.google.com/forum/#!topic/golang-nuts/iUc68Zrxk_c – Christian Nuss Jul 15 '15 at 16:25
  • `go tool cover -h` – 030 May 09 '20 at 12:59