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

proposal: allow imports of _test.go functions and types in different packages. #39565

fernandomachado90 opened this issue Jun 12, 2020 · 3 comments


Copy link

@fernandomachado90 fernandomachado90 commented Jun 12, 2020

What version of Go are you using (go version)?


Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?


What did you do?

In order to make it visible for tests that are located on different packages, I had to move a common database mock to a non _test.go file.

You can view it here:

What did you expect to see?

I wanted to be able to import and use common _test.go functions on different packages.

What did you see instead?

In order to avoid code duplication and reuse the same mock code in other places I have to make it available outside a _test.go file, which works but seems odd.

Copy link

@ianlancetaylor ianlancetaylor commented Jun 12, 2020

It seems to me that using a common package is the right approach.

Presumably when we import a package we don't normally want to be able to refer to testing functions. Even from test code, we normally want to use the standard package API, not a testing API. So if we were to support this, we would need some additional mechanism to say "import the testing API for package foo". But few people would ever use that mechanism.

Since the need is unusual, and there is a way to do it by using a shared package, I don't see a compelling reason to add this feature.

@andybons andybons changed the title Allow imports of _test.go functions and types in different packages. proposal: allow imports of _test.go functions and types in different packages. Jun 15, 2020
@gopherbot gopherbot added this to the Proposal milestone Jun 15, 2020
Copy link

@fernandomachado90 fernandomachado90 commented Jun 16, 2020

Hi @ianlancetaylor, even when I use a shared package I still have to include test-only code along with production code so it can be seen and used by other packages, which seems like a bad practice.

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals Jan 6, 2021
Copy link

@seankhliao seankhliao commented Feb 3, 2021

duplicate of the previously declined #31135 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants