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

testing: _test package runs init() in the containing folder #58823

Closed
bep opened this issue Mar 2, 2023 · 3 comments
Closed

testing: _test package runs init() in the containing folder #58823

bep opened this issue Mar 2, 2023 · 3 comments

Comments

@bep
Copy link
Contributor

bep commented Mar 2, 2023

I'm having problems finding good documentation about the expected behaviour of the _test package, but as I was bitten/surprised by this, it should warrant at least a searchable issue.

See the project and failing test in https://github.com/bep/gotestpackageinit/blob/main/pkga/int_test.go#L1

The test fails because the init() func in pkga panics (which is a little constructed, I agree).

My understanding is that pkga_test would be compiled as a separate package and that I could only access pkga's exported identifiers and only with an explicit import of that package.

In my case I had a great and working integration test of a feature that failed mysteriously in the production binary because the init() was never called.

gohugoio/hugo#10774

@seankhliao
Copy link
Member

From go help test:

Test files that declare a package with the suffix "_test" will be compiled as a
separate package, and then linked and run with the main test binary.

While _test is compiled as a separate package, the test binary itself, including the setup/teardown is run in the context of the main package, so it will always be included.
This is apparent if you write tests in both packages and compile the test binary: you only get one.

Closing as both working as intended and documented

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2023
@bep
Copy link
Contributor Author

bep commented Mar 2, 2023

While _test is compiled as a separate package, the test binary itself, including the setup/teardown is run in the context of the main package, so it will always be included.

@seankhliao I have read your comment twice, and I'm getting even more confused than before I wrote this issue. What main package do you talk about? I have a pkga_test (which gets compiled as a separate package) that invokes a exported method in pkgb.

Test files that declare a package with the suffix "_test" will be compiled as a
separate package, and then linked and run with the main test binary.

There's nothing in the above that suggests the current surprising behaviour – and I'm not thrilled that my issue is brushed off so lightly.

@seankhliao
Copy link
Member

The test binary is constructed in the context of pkga, even if it contains no tests.
Anything in pkga_test are just an additional set of tests to be included.
Initialization is property of the program, not the package, so init is always called.

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

3 participants