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/go: write empty coverage report when there are no tests #18909

Closed
jspiro opened this issue Feb 3, 2017 · 9 comments
Closed

cmd/go: write empty coverage report when there are no tests #18909

jspiro opened this issue Feb 3, 2017 · 9 comments

Comments

@jspiro
Copy link

jspiro commented Feb 3, 2017

tl;dr

  • go test -cover should output coverage for packages with .go files that lack tests to accurately report that there is no test coverage for that package
  • go test should have a flag that causes it to fail if a package is found without any tests, and/or should fail if a .go file is found without associated _test.go

It (sort of, out of lazy convenience) makes sense that go test ./... succeeds if it finds a package with at least one .go file, but without tests, skipping it. But the same behavior doesn't make sense for coverage -- negative coverage reports should be created for packages without a single test but with go files.

The problem: Say you have 100 packages of Go code (each has approximately the same number of statements), and only one has tests, and those tests cover 100% of that package's code, then coverage for the repo will be 100% (only one coverage report is created). But it's really 1% coverage.

If you add an empty _test.go file to each package, you'll suddenly get negative coverage reports for every package, and your coverage will be correct. This is silly, you shouldn't have to remember to do this everywhere to get accurate coverage reports.

Proposal
Either (or both)

  • Add a flag to go test that will fail if a package is discovered without tests (alerting us to the problem so we can add a test, and always get proper coverage)
  • Have go test -cover output negative coverage repots even if no tests are found (or add a flag that will do this)

Nice to have

  • A go test flag that fails if any .go file doesn't have an accompanying _test.go -- I'm sure some people would welcome this level of strictness

Up for debate

  • Whether package main files must also have _test.go -- not sure how prevalent it is to have tests for these. I noticed that go test seems to try running these files if a test file exists; this causes my tests to fail since main packages tend to be commands that fail without input.
  • Another caveat is that generated code like protobufs and mocks generally don't come with tests, so having a way to exclude a package might be necessary. Or maybe you just need to test your code!

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

go version go1.7 linux/amd64

@rsc
Copy link
Contributor

rsc commented Feb 6, 2017

When I run go test in a directory with no code, I get:

$ go test
?   	rsc.io/tmp/asmvet	[no test files]
$ go test -cover
?   	rsc.io/tmp/asmvet	[no test files]
$ 

It's true that the command succeeds, but the output is clear that no tests were run (and has been for at least the last few releases). Making this fail (even optionally) seems like overkill.

We could probably arrange for an empty cover profile to be written from the go command in this case though.

@rsc rsc changed the title Proposal: accurate coverage reports and failed tests when 'no test files' are found proposal: testing: print "no coverage" coverage report for no tests Feb 6, 2017
@rsc
Copy link
Contributor

rsc commented Feb 27, 2017

OK, so if we just write an empty cover profile file (no output change, no exit status change), is that enough, @jspiro? That seems reasonable to me.

@jspiro
Copy link
Author

jspiro commented Feb 27, 2017

@rsc That should work as long as it marks all lines in the package as uncovered.

Two questions remain for debate:

  • Should package main have an empty cover profile? IME they don't usually tested; but this is inconsistent and possibly stylistic.
  • Generated protobuf code typically doesn't come with tests (since it's implicitly tested). Though there isn't way to determine that by package.

Neither is a real problem, since technically those lines of code have no coverage. They're just unlikely to ever get coverage.

@rsc
Copy link
Contributor

rsc commented Feb 28, 2017

Whether those matter depends on what your program does with the 0% coverage profile we're talking about writing out. That's unclear to me, but it seems fine to make sure that 'go test -cover' always writes a coverage profile.

@rsc
Copy link
Contributor

rsc commented Mar 6, 2017

Accepted for writing an empty cover profile file when there are no tests.

@rsc rsc changed the title proposal: testing: print "no coverage" coverage report for no tests proposal: cmd/go: write empty coverage report when there are no tests Mar 6, 2017
@rsc rsc added this to the Go1.9 milestone Mar 6, 2017
@JelteF
Copy link
Contributor

JelteF commented Apr 19, 2017

Would be really great to have this included. Right now I work around this by adding an empty file (except for the package line) called xxx_test.go to packages without tests to work around this.

I would like to note though that there's an important difference between an empty cover profile and a cover profile containing all lines of the files with counts of 0. The one with 0 counts is what is useful, otherwise there is still no data for the cover tools to use. The 0 counts are what my above workaround results in so I doubt it would be hard to achieve that automatically.

@jspiro
Copy link
Author

jspiro commented Apr 19, 2017 via email

@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 Jul 22, 2017
@rsc rsc changed the title proposal: cmd/go: write empty coverage report when there are no tests cmd/go: write empty coverage report when there are no tests Nov 9, 2017
@gopherbot
Copy link

Change https://golang.org/cl/76875 mentions this issue: cmd/go: allow -coverprofile with multiple packages being tested

@thanm
Copy link
Contributor

thanm commented Aug 24, 2023

See also http://go.dev/cl/c/go/+/495447, which I think is relevant.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants