t.Setenv() and t.Chdir() set t.denyParallel to prevent tests from using t.Parallel(), which would cause problems.
It would be useful to be able to mark tests or custom testing helpers as such. For example in some tests I want to os.Unsetenv() a variable, or set a package-level "test hook" variable. These tests can't be run in parallel, but there is nothing preventing this except careful documentation and usage.
The current workaround is t.Setenv("NONEXISTENT", ""), which is ugly and results in a bad error.
Concretely:
// erial marks that this test can only be run in Serial, and
// prevents this test or any of its ancestors to run in parallel.
func (t *T) Serial() {
t.checkParallel()
}
And maybe also changing t.denyParallel to a string with the function name, so the error can be changed from:
const parallelConflict = `testing: test using t.Setenv, t.Chdir, or cryptotest.SetGlobalRandom can not use t.Parallel`
to:
const parallelConflict = `testing: test using %s can not use t.Parallel`
Related:
proposal: testing: ability to tell if test is running in parallel #70017
proposal: testing: run tests in parallel by default #73805
If #73805 is implemented in the future, t.Serial() could be safely changed to mean "don't run this test in parallel" instead "panic if t.Parallel() is called".
t.Setenv() and t.Chdir() set t.denyParallel to prevent tests from using t.Parallel(), which would cause problems.
It would be useful to be able to mark tests or custom testing helpers as such. For example in some tests I want to os.Unsetenv() a variable, or set a package-level "test hook" variable. These tests can't be run in parallel, but there is nothing preventing this except careful documentation and usage.
The current workaround is
t.Setenv("NONEXISTENT", ""), which is ugly and results in a bad error.Concretely:
And maybe also changing
t.denyParallelto a string with the function name, so the error can be changed from:to:
Related:
proposal: testing: ability to tell if test is running in parallel #70017
proposal: testing: run tests in parallel by default #73805
If #73805 is implemented in the future, t.Serial() could be safely changed to mean "don't run this test in parallel" instead "panic if t.Parallel() is called".