Has anyone succeeded in generating code coverage for Go unit tests? I can\'t find a tool for that on the web.
It's right here, some docs here.
$ go tool
6a
6c
6g
6l
addr2line
api
cgo
cov
dist
ebnflint
fix
gotype
nm
objdump
pack
pprof
prof
vet
yacc
$ go tool cov -h
usage: cov [-lsv] [-g substring] [-m minlines] [6.out args...]
-g specifies pattern of interesting functions or files
go tool cov: exit status 1
$
I haven't used it, this is all I know.
If you like to see the uncovered lines by function directly in a terminal I rewrote the cover tool for this purpose. It's available at https://github.com/gregoryv/uncover.
Usage
go get -u github.com/gregoryv/uncover/...
go test -coverprofile /tmp/c.out
uncover /tmp/c.out
Screenshot
Try using gaia-docker/base-go-build Docker Image.
This is a Docker image that contains all you need in order to build and test coverage. Running test coverage inside a Docker container creates .cover folder with test coverage results of your project.
docker run --rm -v "$PWD":$PROJECT_PATH -w $PROJECT_PATH $BUILDER_IMAGE_NAME /go/script/coverage.sh
The test coverage script running on all projects' folders and generates, inside .cover folder junit and coverage reports for each folder, and a combine coverage report of all projects' tests.
Codecov also suggests a script that collect coverage results: multiple files
Note that Go 1.2 (Q4 2013, rc1 is available) will now display test coverage results:
One major new feature of
go test
is that it can now compute and, with help from a new, separately installed "go tool cover
" program, display test coverage results.The
cover
tool is part of the go.tools subrepository. It can be installed by running
$ go get golang.org/x/tools/cmd/cover
The cover tool does two things.
- First, when "
go test
" is given the-cover
flag, it is run automatically to rewrite the source for the package and insert instrumentation statements. The test is then compiled and run as usual, and basic coverage statistics are reported:
$ go test -coverprofile fmtcoverage.html fmt
ok fmt 0.060s coverage: 91.4% of statements
$
Second, for more detailed reports, different flags to "go test" can create a coverage profile file, which the cover program, invoked with "
go tool cover
", can then analyze.
Frank Shearar mentions:
The latest versions of Go (2013/09/19) use:
go test -coverprofile <filename> <package name>
Details on how to generate and analyze coverage statistics can be found by running the commands
$ go help testflag
$ go tool cover -help
Ivan Black mentions in the comments:
go test -coverprofile cover.out
and then
go tool cover -html=cover.out
openscover.out
in your default browser
I don't even want to wait for the browser to open, so I defined this alias:
alias gc=grep -v -e " 1$" cover.out
That I just type gc
, and have a list of all the lines not yet covered (here: with a coverage.out
line not ending with " 1
").
Coverage Report →
a) Run all the tests and enable coverage --> go test ./... -coverprofile coverage.out
b) Get coverage for individual functions as well as overall coverage → go tool cover -func coverage.out
c) See the lines covered and the ones not covered by your tests → go tool cover -html=cover.out -o coverage.html
. Open the coverage.html
file hereby generated in the browser and analyze the detailed coverage info.
Test Coverage for Golang
go get github.com/axw/gocov/gocov
go get -u gopkg.in/matm/v1/gocov-html
Check It is Installed Correctly And you have access from your Terminal
Run the Test Case
If you run the test case it will Reder the .json File Based on the file you will get the Code Coverage Report in .html file
gocov test >your_Coverage_report.json
Once Your Test case is done Generate a Report in .html File using .json
gocov-html your_Coverage_report.json >your_Coverage_report.html
Reference
GoTest Coverage Tool for go lang
Go Test Report Tool
Alternate Method
Go Native Test coverage
go test -coverprofile=coverage.out
go tool cover -html=coverage.out