-
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: add M.Cleanup #42161
Comments
It seems to me that |
I agree with that, but I came to suspect a Cleanup facility may be needed
if a formal Fatal and exit facility is added to testing.M. One of the
triggers for my concern and filing this issue was seeing log.Fatal calls
combined with load-bearing defer blocks that wouldn’t run (defer block to
reconcile external state that the test mutated in its main entry point and
tried to revert).
It seems either better documentation or API could ameliorate this since
keeping the nuance and semantics in mind as tribal or institutional
knowledge simply isn’t sustainable. It also felt wrong to maintain
in-house documentation on this as well.
I trust your folks judgment. :-)
|
It sounds like we don't need to worry about an M.Cleanup until/unless we have M.Fatal etc. |
Over recent quarters, I have seen folks carelessly gravitate toward using
TestMain when it isn’t required (as in, there are better least-mechanism
alternatives like the builtin testing facilities for the task at-hand). I
think it would be useful for TestMain to have some documentation to make
the low-level point you made more clear. Here is a tracer shot: “TestMain
is an advanced feature and not meant for casual use. Regular testing
functions will suffice for most cases.” I don’t mean to bring a point too
orthogonal to the discussion, but perhaps illuminating the intended path of
the user journey in documentation would provide an escape valve alternative
versus expanding the API. What do you think?
|
It's certainly easy to add a sentence or two to testing.M. |
Based on the discussion above, this proposal seems like a likely decline. |
Change https://golang.org/cl/334649 mentions this issue: |
No change in consensus, so declined. |
Reviving an old issue, the use case here is that deferred functions that are called in TestMain do not run if there's a panic in m.Run, even if there's a deferred recover on the stack in TestMain. Any processes started in TestMain don't get cleaned up when the test exits in this case. |
Adding my hat to this. The documentation on It even says so in the documentation However, were you to actually use The best way I found to get around this was to only call retCode := m.Run()
if retCode != 0 {
os.Exit(retCode)
} |
This is a feature request/proposal for
testing.M
. I was doing a Readability Review today of user code. The code for one reason or another was using a custom main entrypoint. I noticed how bare the support intesting.M
is for users to report and handle setup/teardown failures as well as conduct cleanups. The setup, execution, and teardown of the custom test main needed to do both of these tasks. It dawned on me that there no advice is given in the documentation about where setup error messages should go and how to correctly abort (e.g., what exit value).I would like to propose that for similar reasons of
testing.T
having(*testing.T).Cleanup
and(*testing.T).Fatal
,testing.M
should have the same.Between the two of them, the cleanup is less important. But how to report errors and setup failures looks very relevant.
For instance, if a test main fails, should the user call
log.Fatal
or one of its variants, hand emit tostderr
orstdout
? I have seen the whole zoo of behavior before. Is any non-zero exit value OK or only some? I get that custom test mains are an advanced feature and should be seldom used, but it seems potentially like we are not providing the users an adequately documented journey. Maybe this is intentional (e.g., Go tests to support custom test harness and executor requirement contracts)? It does deserve some Schrift at the very least.The text was updated successfully, but these errors were encountered: