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: Types and Functions defined in _test.go files are not visible/exported #10184

Closed
faizkothari opened this issue Mar 17, 2015 · 3 comments

Comments

@faizkothari
Copy link

Go version 1.4.2 release
OS: Linux amd64

The go install command doesn't pick up structs and functions which are defined in source file with names xxx_test.go
I know go test command uses _test.go files as part of the built in testing framework. In case I am building a lib, and I write tests in _test.go files (define some functions etc.) and run them from package main, but at present those definitions won't be visible and go install throws an error: undefined: TypeName
Is it possible that go install/build can be trained to throw a more verbose error message: For example: Definition found in xxx_test.go, please change the file name to something other than xxx_test.go

@faizkothari
Copy link
Author

Ok, upon going through the code, I found these two places:

https://github.com/golang/go/blob/master/src/cmd/go/build.go#L949
https://github.com/golang/go/blob/master/src/cmd/go/test.go#L951

_test.go files are not included in coverage. Fine but is it possible that go install/build can be trained to throw a more verbose error message? For example: Definition found in xxx_test.go, please change the file name to something other than xxx_test.go

@mikioh mikioh changed the title Types and Functions defined in _test.go files are not visible/exported cmd/go: Types and Functions defined in _test.go files are not visible/exported Mar 18, 2015
@attilaolah
Copy link

Apparently this issue has the side effect of resulting in inconsistent test runs whether you run go test against a relative path (like ./...) or an absolute (import-like) path, like github.com/user/pkg.

Namely, if you run go test ./..., and your test files live in package foo_test, and they import foo, but they rely on something exported under *_test.go files (a common example is theexport_test.go, like the one in pkg fmt), go test will import the package from $GOPATH, not from wherever go test ./... is run (which makes sense). However, as @faizoff mentioned, the *_test.go files are not included in those builds (not even in tests builds, when the package being tested is other than the package that is imported, minus the _test suffix).

This usually never happens because people tend to run go test ./... while they're in the source tree. However, it does happen with the new Wercker Ewok, which runs Go tests in a different directory (/pipeline/source). This results in CI tests that use go test ./... failing, while CI tests that use go test github.com/user/pkg/... are passing.

/cc @hatchan

@robpike
Copy link
Contributor

robpike commented Mar 21, 2015

Working as intended. The *_test.go files are compiled into the package only when running go test for that package.

@robpike robpike closed this as completed Mar 21, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
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

4 participants