I would like to run tests in a package in parallel. We know that the testing.Parallel
function enables us to run two tests in a package in parallel, but is there a way to automatically enable all tests in a package to run in parallel without calling t.Parallel()
?
For example, are there any flags I can throw into go test
to make all tests with the function signature func(t *testing.T)
be ran in parallel?
This comes from commit f80d8fb and Go1 (Oct. 2011)
An option (--parallel
) was debated at the time, but quickly rejected:
Many tests won't work if run in parallel. (Many will, too.)
The presence of a--parallel
flag means scripts and so on will want to turn it on, and tests must be able to protect themselves from erroneous runs.I suggest whitelisting individual tests that are knowingly able to run in parallel, and the easiest way to do this is to name them differently.
I like
t.Parallel
.
It's clear which tests are parallel, it's not a global default, so you can apply it just to the tests where it is both appropriate and necessary, and the behavior is great: all the non-parallel run first - meaning the easy ones - and then the parallel ones can chew cpu.