我想在包中并行运行测试。我们知道testing.Parallel
function使我们能够并行运行包中的两个测试,但是有没有一种方法可以自动使包中的所有测试并行运行而不调用 t.Parallel() ?
例如,我是否可以将任何标志放入 go test
中,以使具有函数签名 func(t *testing.T)
的所有测试并行运行?
最佳答案
这来自commit f80d8fb和 Go1(2011 年 10 月)
选项(--parallel
)是debated at the time ,但很快就被拒绝了:
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.
关于go - 如何自动启用测试在包内并行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927397/