-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: testing: define naming convention for test functions #33688
Comments
Seems reasonable to me but it's not clear what would actually change in the Go project. Are you suggesting that we add some comments to the testing package doc explaining that the format used for example names should also be used for test names? |
Yes, that's the suggestion -- a documentation update that implies convention. At the moment the docs are not (to me at least) indicating that they apply to both Tests and Examples. Maybe my interpretation was incorrect? Do they actually already apply to both types? |
The naming conventions only apply to examples, because the naming of examples affects the way that godoc presents the package documentation. Test names do not affect godoc, as godoc doesn't do anything about tests. |
Ah, that makes a lot of sense! The tooling for That's very similar to what's happening on the community side with
|
I have experimented with a few approaches to this: 1a. Test names with underscore suffixes
Notice that even within this arguably easy-to-parse version, I tend to elide the "T_" prefix when I think it doesn't cause confusion, i.e. if there's only one major exported type in the package. Pros:
Cons:
1b. Test names with camelCase suffixes
Pros:
Cons:
2. Additional subtests This looks more like
Pros:
Cons:
I don't find a whole lot to like about 1a, because underscores are so uncommon in Go that they stand out like a sore thumb. I thought I would like 2 but it also makes the Test function giant, and can result in suppressing a ton of output if one of the subtests times out. For my part, I think I tend to mix and match these days. I'll gravitate to 1b when there's usually only one test for the "thing" in the package, and to 2 if there are a lot of interrelated tests that share setup. I do appreciate that both of these make it significantly more difficult for tooling to draw the 1:1 relationships, however. I have been writing Go for a really long time and don't really know if I have a good idea of what the right pattern is here. In code reviews, I more-or-less let my teammates pick whatever approach makes sense to them, and I just focus on making sure their failure messages are easy to understand. If the motivation here is to help out IDEs that are looking for tests, you could approach it from a slightly different direction, and develop heuristics based off of tests that pass table fields to the function, name the function in error messages, refer to the function in the doc comment, and/or that just call the function syntactically within the {}s of the test/case. |
Test functions should have names that are meaningful to the user, not dictated by some inflexible tool. We shouldn't retroactively invalidate all the names people have picked. Tools that care about matching code to test functions could look at which functions are called. |
Regarding the proposal, I was hoping more to see a convention in the language that tools could then build against, similar to how Examples and Benchmarks have their own [strict] conventions. The functionality the tools add wouldn't affect any existing test methods; they just would preserve current state. Features that the tools introduce based on the convention would be expected to cleanly handle (or ignore) method names which don't give the tools the pattern they need in order to introspect on the tests' target. I think that automatically discovering that based on the functions that are called would serve the same purpose! But...I'm having a hard time seeing how that would work? How could a tool (or |
Benchmarks have no strict convention either.
This is not really a well-formed question. Many tests test multiple functions. Only the simplest tests focus on just one function. To the extent that that happens, it is often TestF to test the function F. But it's not a "defined" or required convention. This seems like a likely decline (infeasible). |
No change in consensus, so declining. |
This proposal is for eliciting discussion around the current absent naming convention for test functions. This should not involve any changes to the language specification itself but instead serve as a guidepost for community tooling and best practices.
Background
There is precedent within the community and within the Go packages themselves to name test functions in a specific way based on their definitions. Two primary cases demonstrate this.
gotests
The popular gotests tool defines an informal specification for naming tests and uses it to auto-generate table driven tests from a given function definition. It appears to be widely accepted and used among the IDE community.
If accepted into the main specification, the IDE integrations using this tool could be further developed with additional features such as "Jump to Test for Function" / "Jump to Function for Test". Without a specification these features would be based upon an informal specification and subject to potential breakage if the community shifts to another upstream test generation tool.
(Disclaimer -- this issue came from a discussion on gotests: cweill/gotests#106)
testing examples
In the go source for the
testing
package a naming convention has already been defined for "Example" test cases. Extracting the content from those docs for clarity:Having a naming convention for Example test functions gives a solid starting point if we decide to move forward for a standard Test function.
Proposal
I would stop short of officially recommending a naming convention for Go to adopt, but encourage that the discussion start based on the existing examples we are seeing in
gotests
and in the Example function naming conventions. Looking forward to what everyone thinks!The text was updated successfully, but these errors were encountered: