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
proposal: cmd/go: cross-package test imports #44086
Comments
You could make an internal package, like
How would you share code that requires private access to multiple packages to begin with? Personally, what I do is keep some helper test code in an internal package, then use it from each test package with whatever access to private code is necessary. |
Thanks for the suggestion, my understanding is that this is currently the idiomatic way to deal with shared test logic. I think it can be improved :)
This is really the heart of the issue for me. What if this I don't want the consumer to know about this detail, but a helper function that is part of the test package should be able to set it directly. Today that sort of thing isn't possible without making that helper function part of the public package interface. |
You could instead put everything in |
This proposal is a duplicate of a previously discussed proposal, as noted above, |
Problem
Occasionally two related packages have some overlap in their test code. A common example would be a package defining a complex core data type and the manipulation of that data, and the consumer of that data type which may exist in many other packages.
The core data type may have extensive tests to verify the different ways it can be manipulated and ensure that it contains the correct values.
Consumers of that data type may want to make use of those test objects rather than go through the same process of recreating them.
Currently the recommendation is the put this common test code in a common non _test package, so that it can be used in multiple places. This works but breaks the namespacing. What if there are only certain parts of the core data type which should be public? Or if the tests require private access? What used to be a single
core_test.go
file is now split across multiple packages instead of using the same idioms we use when writing non-test code.Proposal
Put test code in an implicit
_test
package which can be imported to other_test
packages.Using the
core
andconsumer
examples, perhaps we havecore_test.go
which is part of thecore
package. It defines several useful functions thatconsumer_test.go
would like to use. So we simply addcore_test
to get them.The text was updated successfully, but these errors were encountered: