-
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
proposal: testing: add Count function to return the test.count value #64883
Comments
single setup should generally be done by other primitives, like Having test behaviour change based on the number of times it runs seems like bad practice and defeats the point of running it multiple times. |
One clear example is a test that depends on global state and can only run once. So for example, the first time it runs, it would proceed as expected, but if |
that doesn't really support have a Count() function? |
Another good reason: If you're testing a large project that has |
That doesn't sound like a good reason to have Count, instead it sounds like one to not use init() (or use init only for stuff that really only does need to be initialized once, no matter how tests are run) |
On Fri, Dec 29, 2023 at 2:19 PM Sean Liao ***@***.***> wrote:
That doesn't sound like a good reason to have Count, instead it sounds like one to not use init() (or use init only for stuff that really only does need to be initialized once, no matter how tests are run)
It turns out there are situations when you want to test things that
happen in init() more than once!
|
I still don't see how Count helps, if you're aware of the issue, you can implement workarounds (reexec), if you're not aware, then Count doesn't help you atl all |
This would be useful for https://github.com/gkampitakis/go-snaps in order to support having multiple snapshots in a single test while still comparing within the same count run. In order to provide support for multiple snapshots, it uses a Exposing the |
I don't understand how this helps. Instead of checking whether testing.Count() > 1 you can just count the number of times the code has been run. Also if -count=2 then testing.Count()==2 both times, so you can't distinguish the first. |
This is a good workaround, and if I count and have testing.Count(), I can know this is iteration N of M. I would also have just expected the value would be there by symmetry arguments since I also realize it's a niche request. Thanks for looking! |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Proposal Details
In golang 1.21.0 a new
Testing
function was added to the"testing"
package. I propose a similar and newCount
function which along the same lines, returns thetest.count
value for the test. Implementation is similar toShort
shown here:This would be very useful to allow a test to perform some more expensive global setup only once if it's going to be run multiple times, for example. Or to know how much longer we expect a test to last. (Eg: time*count)
Thanks!
The text was updated successfully, but these errors were encountered: