Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang - codecoverage always shows coverage: 0.0% of statements

I created one sample go project and created a unit test cases for the same (In Linux environment, go1.3 version)

When i ran go test the output would be

PASS
ok supported_db 0.201s

And i tried to perform code coverage for whole application by using go test -cover command it shows

go tool: no such tool "cover"; to install:
go get code.google.com/p/go.tools/cmd/cover

Also i checked the coverage while running a specific test case by running go test -cover CouchDB_test.go command it shows

ok command-line-arguments 0.158s coverage: 0.0% of statements

please help me to run code coverage in golang.

like image 984
Bathakarai Avatar asked Sep 01 '14 11:09

Bathakarai


2 Answers

It helped me

Just add -coverpkg=./... option if tests are in sub folders:

go test ./... -v -coverpkg=./...
like image 118
Nick Avatar answered Oct 02 '22 10:10

Nick


Try first:

go test -coverprofile=coverage.out

I then run, to see the result:

go tool cover -html=coverage.out

If the 1.3 version was installed through an upgrade of 1.1, 1.2, ..., you can try, as in issue 110:

I solved this by completely removing $GOPATH/src/code.google.com/p/go.tools and install cover again:

go get golang.org/x/tools/cmd/cover
like image 31
VonC Avatar answered Oct 02 '22 12:10

VonC