testinggocover

Golang coverprofile output format


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?

Solution

  • 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