-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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: clarify when TestMain is appropriate and not #44200
Comments
The main argument for Personally I would say that errors in I'm not sure any of this needs to go into the docs, but a blog post would be fine. |
Would an example code body or similar suffice? We're going to take care of this, I expect, in our internal documentation, but I'd hate to leave folks wondering. I've seen a zoo of practices around use over the years, some of which have been wrong. There are nice soft ways that counsel above could be phrased without requiring folks to look at previous issue IDs. What do you think? |
I'm not sure how to write an example for Do you not like the idea of a blog post? It might be similar to the one for contexts (https://blog.golang.org/context), in the sense of a blog post describing the right way to use a library feature. |
Don't get me wrong; I like the idea of blog posts. I've grown
preference-wise very attached to the native API documentation as a place to
find canonical information and patterns. I trust in terms of your
recommendation on a blog post so won't push more. I will talk to Jean de
Klerk about this one since he has experience with writing them. It is
probably relatively easy to explain this advice as a blog post since they
often take on a rhetorical-storytelling form. I'll ruminate further.
… |
IMO an |
Change https://golang.org/cl/334649 mentions this issue: |
Beginner and intermediate Go users periodically use TestMain when requirements do not necessitate TestMain (exceeding least-mechanism design). This commit expands package testing's documentation to convey that the TestMain feature itself is somewhat low-level and potentially unsuitable for casual testing where ordinary test functions would suffice. Fixes #42161 Updates #44200 Change-Id: I91ba0b048c3d6f79110fe8f0fbb58d896edca366 Reviewed-on: https://go-review.googlesource.com/c/go/+/334649 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
There is also the issue whereby a singleton object is required by the tests. |
We had an open discussion in a longer design review thread about when user-authored custom
TestMain
(debuted in release 1.4) entrypoints are necessary or not compared tofunc init
. We were able to offer at best fuzzy criteria for this. It would be beneficial for the newcomer's perspective if some basic litmus tests, advice, or criteria were provided. I have no opinion on where but rather that they are central and findable.I don't want to bike shed the reasoning, but I will enumerate some of the tradeoffs I heard:
func init
semantics, so that speaks in favor offunc init
for setup. One counterpoint is that a setup routine that requires all package initializers to have been run by the time it is executed could become flaky or error-prone.-test.run=pattern
is used). This also speaks againstfunc init
above.func init
orfunc TestMain
, could imply mutated global state. Unless the tests use whatever is setup in a hermetic way individually, it could accidentally encode test ordering assumptions between test functions. Might be better to encourage tests to locally set up what they need versus not and to enforce cleanup, especially now that we have(*testing.T).Cleanup
.One final ancillary point I heard in an earlier discussion related to user-authored
TestMain
that a documentation treatment should also cover:TestMain
?panic
,os.Exit(1)
,log.Fatal
?stdout
orstderr
?Before stating that the answers to the above is obvious, it may be obvious to a seasoned practitioner but not a newcomer.
The text was updated successfully, but these errors were encountered: