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: cmd/go: cross-package test imports #44086

Closed
winder opened this issue Feb 3, 2021 · 5 comments
Closed

proposal: cmd/go: cross-package test imports #44086

winder opened this issue Feb 3, 2021 · 5 comments

Comments

@winder
Copy link

winder commented Feb 3, 2021

Problem

Occasionally two related packages have some overlap in their test code. A common example would be a package defining a complex core data type and the manipulation of that data, and the consumer of that data type which may exist in many other packages.

The core data type may have extensive tests to verify the different ways it can be manipulated and ensure that it contains the correct values.

Consumers of that data type may want to make use of those test objects rather than go through the same process of recreating them.

Currently the recommendation is the put this common test code in a common non _test package, so that it can be used in multiple places. This works but breaks the namespacing. What if there are only certain parts of the core data type which should be public? Or if the tests require private access? What used to be a single core_test.go file is now split across multiple packages instead of using the same idioms we use when writing non-test code.

Proposal

Put test code in an implicit _test package which can be imported to other _test packages.

Using the core and consumer examples, perhaps we have core_test.go which is part of the core package. It defines several useful functions that consumer_test.go would like to use. So we simply add core_test to get them.

@gopherbot gopherbot added this to the Proposal milestone Feb 3, 2021
@mvdan
Copy link
Member

mvdan commented Feb 3, 2021

What if there are only certain parts of the core data type which should be public?

You could make an internal package, like internal/testcommon - but with a better name.

Or if the tests require private access?

How would you share code that requires private access to multiple packages to begin with?

Personally, what I do is keep some helper test code in an internal package, then use it from each test package with whatever access to private code is necessary.

@winder
Copy link
Author

winder commented Feb 3, 2021

What if there are only certain parts of the core data type which should be public?

You could make an internal package, like internal/testcommon - but with a better name.

Thanks for the suggestion, my understanding is that this is currently the idiomatic way to deal with shared test logic. I think it can be improved :)

Or if the tests require private access?

How would you share code that requires private access to multiple packages to begin with?

This is really the heart of the issue for me. What if this core object requires many state transitions to reach the interesting test cases? It may be easier and less error-prone to set a private type directly rather than trying to carefully recreate the chain of events that lead to the state (carefully re-creating the state would be a different test).

I don't want the consumer to know about this detail, but a helper function that is part of the test package should be able to set it directly. Today that sort of thing isn't possible without making that helper function part of the public package interface.

@seankhliao
Copy link
Member

You could instead put everything in internal/ packages and use a public package to export only the parts of your API that you want your users to have access to.

@seankhliao
Copy link
Member

also duplicate of #31135 #39565

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Feb 4, 2021
@rsc rsc changed the title proposal: cross package test imports proposal: cmd/go: cross-package test imports Jul 20, 2021
@rsc rsc moved this from Incoming to Declined in Proposals (old) Jul 28, 2021
@rsc
Copy link
Contributor

rsc commented Jul 28, 2021

This proposal is a duplicate of a previously discussed proposal, as noted above,
and there is no significant new information to justify reopening the discussion.
The issue has therefore been declined as a duplicate.
— rsc for the proposal review group

@rsc rsc closed this as completed Jul 28, 2021
@golang golang locked and limited conversation to collaborators Jul 28, 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