satoru
satoru

Reputation: 33215

"go test" reports incorrect statement coverage

I have a package with only two Go file: one defines the main function and the other is for the tests.

Now assume that I have cd into this package and run the following command:

$ go test -cover
PASS
coverage: 41.8% of statements
ok      github.com/suzaku/dummage       0.010s

As you can see, this works correctly.

But I want to generate a HTML report, so after some googling I use the following command:

$ go test -run=Coverage -coverprofile=c.out github.com/suzaku/dummage
ok      github.com/suzaku/dummage       0.010s  coverage: 1.8% of statements

Note that this time the coverage drops to 1.8%.

What can I do to fix this?

Upvotes: 0

Views: 1185

Answers (1)

serejja
serejja

Reputation: 23841

Are you sure you need that -run=Coverage flag in your go test? This means it will only run tests that match Coverage. If you just want to generate a cover profile for that tests, run go test -coverprofile c.out github.com/suzaku/dummage. Then you may run go tool cover -html c.out to see the HTML report.

If you added -run=Coverage on purpose, then it's expected behavior - the amount of code that runs during -run=Coverage is much less than while running all tests, and the test coverage is calculated for the entire package.

Upvotes: 3

Related Questions