Code Coverage Reports for Golang Applications

This document covers how to generate HTML Code Coverage Reports for Golang Applications.

Generate a test executable which calls your main()

$ go test -c -covermode=count -coverpkg ./...

Run the generated application to produce a new coverage report

$ ./sms.test -test.run "^TestMain$" -test.coverprofile=coverage.cov

Run your unit tests to produce their coverage report

$ go test -test.covermode=count -test.coverprofile=unit.out ./...

Merge the two coverage Reports

$ go get github.com/wadey/gocovmerge
$ gocovmerge unit.out coverage.cov > all.out

Generate HTML Report

$ go tool cover -html all.out -o coverage.html

Generate Function Report

$ go tool cover -func all.out