简体   繁体   English

尝试与 golang testify/suite 并行运行测试失败

[英]Trying to run tests in parallel with golang testify/suite fails

I have several tests using testify/suite package and I execute them in parallel as follows我有几个使用 testify testify/suite package的测试,我按如下方式并行执行它们


type IntegrationSuite struct {
    suite.Suite
}

func TestIntegrationSuite(t *testing.T) {
    suite.Run(t, &IntegrationSuite{})
}

func (is *IntegrationSuite) TestSomething() {
    is.T().Log("\tIntegration Testing something")
    for i := range myTestTable {
        i := i
        is.T().Run("Testing "+myTestTable[i].scenarioName, func(_ *testing.T) {
            is.T().Parallel()
...

func (is *IntegrationSuite) TestSomethingElse() {
    is.T().Log("\tIntegration Testing something else")
    for i := range myOtherTestTable {
        i := i
        is.T().Run("Testing "+myOtherTestTable[i].scenarioName, func(_ *testing.T) {
            is.T().Parallel()
...
        })

However this panics with然而,这恐慌与

panic: testing: t.Parallel called multiple times [recovered]
        panic: testing: t.Parallel called multiple times

How can one leverage parallelism with the specific package?如何利用特定 package 的并行性?

You're calling t.Parallel() on the wrong instance of testing.T .您在testing.T的错误实例上调用t.Parallel()

You're spinning up multiple subtests using:您正在使用以下方法启动多个子测试

is.T().Run("Testing "+myOtherTestTable[i].scenarioName, func(_ *testing.T) {...}

But instead of marking the subtest as parallel, you're marking the outer parent test as parallel for every subtest you start:但是,您不是将子测试标记为并行,而是将外部父测试标记为您开始的每个子测试的并行:

is.T().Parallel() // marks outer test as parallel

Instead, bind the subtest func's testing.T param to a variable and call Parallel on the subtest:相反,将子测试函数的testing.T参数绑定到一个变量并在子测试上调用Parallel

        is.T().Run("Testing "+myOtherTestTable[i].scenarioName, func(t *testing.T) {
            t.Parallel() // this is the subtest's T instance
            // test code
        })

This marks all subtests as parallel within the parent test.这将所有子测试标记为父测试中的并行。 If you also want to mark the parent test as parallel with other tests in the package, call is.T().Parallel() once at the beginning of TestSomethingElse (not within the subtest)如果您还想将父测试标记为与 package 中的其他测试并行,请在 TestSomethingElse 开头调用一次TestSomethingElse is.T().Parallel() (不在子测试内)

For more details on parallelism of subtests, see: Control of Parallelism有关子测试并行性的更多详细信息,请参阅:并行性控制

Please note that the testify suite is currently not "thread safe" and therefore does not support concurrency.请注意,testify 套件目前不是“线程安全的”,因此不支持并发。 There are cases in which tests pass successfully even though they should fail.在某些情况下,即使测试应该失败,测试也会成功通过。

Please find the current status as well the reported issues here: https://github.com/stretchr/testify/issues/187请在此处找到当前状态以及报告的问题: https://github.com/strethr/testify/issues/187

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM