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

Open
fernandomachado90 opened this issue Jun 12, 2020 · 2 comments
Labels
Milestone

Comments

@fernandomachado90
Copy link

@fernandomachado90 fernandomachado90 commented Jun 12, 2020

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

1.14

Does this issue reproduce with the latest release?

Yes.

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

GOOS="darwin"
GOARCH="amd64"

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:
https://github.com/fernandomachado90/go-transactions/blob/master/core/database_mock.go

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.

@ianlancetaylor
Copy link
Contributor

@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
@gopherbot gopherbot added the Proposal label Jun 15, 2020
@fernandomachado90
Copy link
Author

@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.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.