-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: TestMain should not require os.Exit #34129
Comments
Related: #9917. One possibility, that I thought was discussed before but I can't find an issue, is to change the behavior depending on the result of |
Ah, thanks. |
This mistake does happen a lot, and the testing package that called TestMain and prepared the m can certainly record the result of m.Run for use in its own outer os.Exit. Letting TestMain simply call m.Run instead of calling os.Exit(m.Run) seems OK and makes defer more useful. I'm not as confident about alternate TestMain signatures; that seems unnecessary to solve this specific problem. |
No real insight from me. The topic seems covered by @ianlancetaylor's and @rsc's points. |
It seems like if we do the "automatic os.Exit" that will fix quite a few buggy tests, and it basically eliminates the need for a TestMain that returns int. We could always revisit that and add it later, if needed, but let's start with the automatic os.Exit. To be clear, the proposal is this. Right now, the overall test func main calls TestMain(m), and the TestMain function is expected to do two things:
The proposal is that:
This would mean that implementations of TestMain that forget to call os.Exit will have it called for them. And implementations that want to explicitly avoid os.Exit in order to make defer useful can do that. If TestMain does not call m.Run (presumably for a good reason) and returns, then the outer func main should os.Exit(0) as it does today. Does anyone object to this plan or think it would not address the necessary use cases? |
This seems like a likely accept. Leaving open a week for final comments. |
No new comments in the past week, so marking as accepted. |
Change https://golang.org/cl/219639 mentions this issue: |
Change https://golang.org/cl/221477 mentions this issue: |
In CL 219639 the generated testing package gets a dependency on reflect. Trim that dependency in the go/packages tests, just as we trim other dependencies of the generated testing package. Updates golang/go#34129 Change-Id: I5e2578fc61cafb4d524d0a58525cf78a402c1143 Reviewed-on: https://go-review.googlesource.com/c/tools/+/221477 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
Change https://golang.org/cl/221657 mentions this issue: |
Explicitly listing the pruned packages ties the test to the details of how the testing package is implemented. That shouldn't matter, and it changes in CL 219639. This change makes it possible to work whether or not CL 219639 is committed. Updates golang/go#34129 Change-Id: I45875dadeaac42a1002e1329a1a762e340c5352c Reviewed-on: https://go-review.googlesource.com/c/tools/+/221657 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Changkun Ou <euryugasaki@gmail.com> Reviewed-by: Michael Matloob <matloob@golang.org>
Defers will not be run when using os.Exit (golang/go#34129) Signed-off-by: Alexander Obukhov <dev@sprql.space>
Defers will not be run when using os.Exit (golang/go#34129) Signed-off-by: Alexander Obukhov <dev@sprql.space>
Defers will not be run when using os.Exit (golang/go#34129) Signed-off-by: Alexander Obukhov <dev@sprql.space>
TestMain
is expected to callos.Exit
itself. This leads to subtle issues withdefer
andpanic
that leads to much headscratching. I'm proposing that ifTestMain
doesn't callos.Exit
, the testing framework will call it for you afterTestMain
returns.Let's say you start here:
Subtle bug #1: your teardown isn't running; you're making a mess of your test environment. Try this:
Oops, setup has a
panic
call. Now when you run go test, it's silent..Uh, headscratch. "Why isn't go running my tests?", you ask. Oh,
os.Exit
is hiding my panic call. Ok, finally:Let's save people some headscratching (and time and effort and silly wrapper code). The testing framework probably has enough information to call
os.Exit
appropriately for you after TestMain returns, right? Why require users to call it?TestMain is really useful for writing end-to-end tests, where a single setup/teardown for all the tests is important. If I break those tests into packages for organization, it would be nice not to copy that
TestMain
wrapper code everywhere.The text was updated successfully, but these errors were encountered: