-
Notifications
You must be signed in to change notification settings - Fork 18k
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: Cannot exclude file from tests using +build !test #12120
Comments
It's not a build constrant/tag named
I want to share an opinion related to that. I consider that a feature. I think it's great you (or anyone) can't exclude some .go files from tests. That makes test code purely additive and easier to reason about.
I think those situations are very rare, and they're best handled as special cases. For example, they can check the binary name for ".test" or ".test.exe" suffix to find out if it's a test running, since Go names the test binaries with a ".test(.exe)" suffix. From
|
It would also be useful to be able to use
A file with such a tag would be included in the build under This tag would enable test data and/or helper functions to be shared across related packages. Currently the only way to share test data/code with another package is to remove the Here's a specific example of how this is useful. In my current project I have the following packages:
Within the |
Allowing // +build test to selectively include new files when testing
is achievable with the package under test only (see various export_test.go
in the standard library.)
I don't think that's we should allow that for imported packages of
a test, because that means you sometimes have to rebuild some
imported packages just to test a package. (Note that we don't
cache compiled test packages.) What's even more dangerous is
that this feature could be used to change the behavior of a package
depending on whether it's used in a test of other package or not,
which might lead to bugs very hard to track down (a bug that only
happens in tests, or worse, that only happens when the package
is not used not in tests.)
The way to handle your case is to have separate test helper package
for package models.
|
Here's what I finally resorted to in order to be able to share my test data and helper functions with other packages.
Thus, my test code is always available when I'm testing (and even when I'm not), but doesn't get included in released binaries. This is a hack, but I decided this was a better solution than either:
Supporting I don't see how your suggestion to create a "separate test helper package" would help. Regardless of whether the code lives in my current I'm also somewhat skeptical about this being as dangerous as you say. Did you have a specific example in mind of how this could result in "a bug that only happens in tests, or worse, that only happens when the package is not used not in tests"? |
As I understand the suggestion, the whole point of |
Thanks for 'splaining! I read the proposal on To provide a bit more context, the application I'm working on runs in industrial gateways (specialized wifi routers that also have cellular data capabilities). Some of these devices have as little as 64MB of free RAM and ~80MB flash when we install our binary. Hence we can't afford to bloat the binary with a bunch of dead test code/data. I'm happy enough with the workaround I described above...just wanted to chime in with another use case for a |
That criteria is satisfied, unless I'm mistaken. To elaborate, suppose you have packages So, you'll end up having:
Packages |
This is working as designed. |
Under normal circumstances you would place test code into a file matching the
test
build constraint/tag, e.g.my_test.go
. The naming convention of the file has led me to believe that this is a build constraint/tag.However, it is not possible to exclude some code from being built into tests ran by
go test
, because writing a file likefoo.go
:Will always compile under
go test
. This is a shame because there are some situations that do warrant different behavior for testing, and this makes it hard to deal with such situations.The text was updated successfully, but these errors were encountered: