Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cover: go test -cover and go tool cover -func don't give the same results #20515

dlespiau opened this issue May 28, 2017 · 2 comments


Copy link

@dlespiau dlespiau commented May 28, 2017

What version of Go are you using (go version)?

go version go1.8 linux/amd64

What did you do?

I used the package for the example below, but the same issue can be reproduced on other packages I have around and so assuming it's a general problem:

$ go test -coverprofile=profile.cov
coverage: 26.3% of statements


$ go tool cover -func=profile.cov | tail -1
total:							(statements)		26.1%

Not that I've noticed larger differences, up to 1% on other packages.

What did you expect to see?

I expect to see the same coverage number.

What did you see instead?

Two different coverage percentages.

@ALTree ALTree changed the title go test -cover and go tool cover -func don't give the same results cmd/cover: go test -cover and go tool cover -func don't give the same results May 28, 2017
Copy link

@gopherbot gopherbot commented May 28, 2017

CL mentions this issue.

Copy link

@dhananjay92 dhananjay92 commented May 28, 2017

Found the issue.

cmd/cover/func.go initializes total to 1 for each function. Whereas testing/cover.go only does it once. So if your code has lots of functions with total==0, then discrepancy is going to be high. should fix that.

@dhananjay92 dhananjay92 added this to the Go1.9Maybe milestone May 28, 2017
dlespiau pushed a commit to dlespiau/covertool that referenced this issue May 29, 2017
This command computes the coverage percentage of the whole profile.

This is different from what go tool cover -func does because of this
bug: golang/go#20515

Signed-off-by: Damien Lespiau <>
@gopherbot gopherbot closed this in c82a630 Jun 2, 2017
@golang golang locked and limited conversation to collaborators Jun 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.