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

Closed
dlespiau opened this Issue May 28, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@dlespiau
Contributor

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 github.com/dlespiau/x86db 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
PASS
coverage: 26.3% of statements

Whereas:

$ 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 from go test -cover and go tool cover -func don't give the same results to cmd/cover: go test -cover and go tool cover -func don't give the same results May 28, 2017

@gopherbot

This comment has been minimized.

Show comment
Hide comment

CL https://golang.org/cl/44337 mentions this issue.

@dhananjay92

This comment has been minimized.

Show comment
Hide comment
@dhananjay92

dhananjay92 May 28, 2017

Member

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.

http://golang.org/cl/44337 should fix that.

Member

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.

http://golang.org/cl/44337 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

Damien Lespiau
coverage: Add a coverage command
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 <damien.lespiau@intel.com>

@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.