-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/go: test -cover confused by import "." in xtest #15795
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
Comments
test -cover
does not include xtests (in _test
package) in coverage percentage or profiletest -cover
does not include xtests (_test
package) in coverage percentage or profile
@robpike, are you still the cmd/cover maintainer? |
Where is the comment I wrote when I added the WaitingForInfo tag? What it said was this: Cover handles this situation correctly in packages such as fmt. And I tried changing encoding/binary to have both internal and external tests, and cover got the same result both times. So I don't understand the problem. Can you please create a reproducing example that is self-contained? |
@leighmcculloch, the problem is caused by the
to
and then you don't need to change anything else (in particular you don't have to put a "static." prefix on every identifier). That produces the right coverage percentages. If instead I do
then I get the result you describe, where the coverage percentage does not include all the lines in the test. I can't remember where the issue is about disallowing import of ".", but this may be an argument one way or the other in that issue. @robpike, no need to work on this. |
test -cover
does not include xtests (_test
package) in coverage percentage or profileWhat === Replaced use of `import "."` with `import "github.com/leighmcculloch/static"` in xtests. Why === The go cover tool does not include tests that have imported the package into xtests (`static_test`) using the dot syntax. Ref: golang/go#15795 (comment)
What === Replaced `package static` with `package static_test` in tests that are testing entirely exported functionality. Why === Blackbox testing is preferred as it results in tests that use the package in the exact same way that another developer would use the package. Tests were originally written this way but it was changed when it was discovered that the go cover tool was not including blackbox tests in the coverage percentage. According to golang/go#15795 the tests were not being included in the package as because the package had been included into the blackbox tests using the dot syntax (`import "."`). Importing using the fully qualified package name fixes this, which is how the blackbox testing imports has been implemented in this commit.
Ah fantastic. Thank you!!! I can confirm replacing
I will avoid use of |
What === Replaced `package static` with `package static_test` in tests that are testing entirely exported functionality. Why === Blackbox testing is preferred as it results in tests that use the package in the exact same way that another developer would use the package. Tests were originally written this way but it was changed when it was discovered that the go cover tool was not including blackbox tests in the coverage percentage. According to golang/go#15795 the tests were not being included in the package as because the package had been included into the blackbox tests using the dot syntax (`import "."`). Importing using the fully qualified package name fixes this, which is how the blackbox testing imports has been implemented in this commit.
CL https://golang.org/cl/31821 mentions this issue. |
Version of Go
1.6
, also replicated ondevel +4b6e560
.Operating system and processor architecture
Operating System:
darwin
Processor Architecture:
amd64
What I did
git clone https://github.com/leighmcculloch/static.git
git checkout with-xtests
go test -cover
git diff with-xtests..with-tests
– change the tests package fromstatic_test
tostatic
git checkout with-tests
go test -cover
What I expected to see
I expected to see a coverage percentage for the
static
package that included the test files that are in the same directory but have their package at the top of their files namedstatic_test
. I expected that it wouldn't matter if the tests were in the packagestatic_test
orstatic
, the coverage should be the same.Additionally expected that looking at the coverage in the HTML format would show most lines green.
What I saw instead
The coverage percentage for the
static
package is only20.4%
and is not including tests that are named as being in thestatic_test
package, even though those tests are in the same directory as thestatic
package and are being executed.When I change the package the tests live in from
static_test
tostatic
, they are included in the coverage and the coverage is93.9%
.Additionally saw that looking at the coverage in the HTML format showed most lines red.
What I've attempted so far to fix it
I've looked (naively) at the source of cmd/go/test.go and I can see the tests grouped in
pxtest
are not recompiled, there are no coverVars associated with pxtest. I attempted to fix this by adding thep.GoFiles
to thepxtest
scoverVars
but I ended up with errors like the below. I'm guessing this is because there's an assumption being made somewhere that files included in coverage are in the same package as the tests.I plan to continue looking at this in more depth, but hoped I could get some suggestions, pointers or confirmation of where I should be looking.
The text was updated successfully, but these errors were encountered: