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: testing: allow importing API from _test.go functions in other tests #39565

Closed
fernandomachado90 opened this issue Jun 12, 2020 · 5 comments

Comments

@fernandomachado90
Copy link

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

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
@fernandomachado90
Copy link
Author

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 (old) Jan 6, 2021
@seankhliao
Copy link
Member

duplicate of the previously declined #31135 ?

@rsc
Copy link
Contributor

rsc commented Jul 14, 2021

It is common for packages to expose standard API that is mainly useful for testing.
That just goes in the non-_test.go files.
This does in fact appear to be a duplicate of #31135.

@rsc rsc changed the title proposal: allow imports of _test.go functions and types in different packages. proposal: testing: allow importing API from _test.go functions in other tests Jul 14, 2021
@rsc rsc moved this from Incoming to Declined in Proposals (old) Jul 14, 2021
@rsc
Copy link
Contributor

rsc commented Jul 14, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Jul 14, 2021
@golang golang locked and limited conversation to collaborators Jul 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants