go test ./... -cover shows 0.0%

707 views
Skip to first unread message

t...@giantswarm.io

unread,
Feb 12, 2016, 11:07:23 AM2/12/16
to golang-nuts
Hello golang folks. I explored a weird behavior when testing go code. There is a test coverage feature and I wanted to use it. When I write tests I define these in a separate packages for isolation. So lets say I test package `foo` which is placed in `foo.go`, then my tests are within package `foo_test` which is placed in `foo_test.go`. Note that I need to import `foo` into `too_test` on purpose. Now the tests are fine but the coverage is `0.0%`, which is simply wrong. When I change the test package name from `foo_test` to `foo` and don't import `foo` into `foo_test` the coverage shows a valid result. Does somebody know why that is? Is it expected? Is it a bug?

Thanks for listening.

Dave Cheney

unread,
Feb 12, 2016, 12:58:32 PM2/12/16
to golang-nuts
Is there a repository available that demonstrates the problem?

Thanks

Dave

t...@giantswarm.io

unread,
Feb 15, 2016, 6:07:17 AM2/15/16
to golang-nuts
Thanks for your quick reply, Dave. I missed to come back due to my notification settings. I just created a simple example to demonstrate the case. Check https://github.com/xh3b4sd/go-test

Thanks

Tim

Dave Cheney

unread,
Feb 15, 2016, 6:30:47 AM2/15/16
to golang-nuts, t...@giantswarm.io
Works for me

zapf(~/src/github.com/xh3b4sd/go-test) % go test ./... -cover

ok      github.com/xh3b4sd/go-test/failing      0.007s  coverage: 100.0% of statements

ok      github.com/xh3b4sd/go-test/working      0.009s  coverage: 100.0% of statements


I noticed your makefile uses symlinks. Symlinks are absolutely prohibited inside gopath, that may be the cause of the problem.

Tim Schindler

unread,
Feb 15, 2016, 6:47:18 AM2/15/16
to golang-nuts, t...@giantswarm.io
Oh, nice. Thanks for making that clear. The ugly linking trick is to circumvent the need of a specific directory structure outside my project. Different story though. Thanks anyway. 
Reply all
Reply to author
Forward
0 new messages