簡體   English   中英

嘗試與 golang testify/suite 並行運行測試失敗

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

我有幾個使用 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()
...
        })

然而,這恐慌與

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

如何利用特定 package 的並行性?

您在testing.T的錯誤實例上調用t.Parallel()

您正在使用以下方法啟動多個子測試

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

但是,您不是將子測試標記為並行,而是將外部父測試標記為您開始的每個子測試的並行:

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

相反,將子測試函數的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
        })

這將所有子測試標記為父測試中的並行。 如果您還想將父測試標記為與 package 中的其他測試並行,請在 TestSomethingElse 開頭調用一次TestSomethingElse is.T().Parallel() (不在子測試內)

有關子測試並行性的更多詳細信息,請參閱:並行性控制

請注意,testify 套件目前不是“線程安全的”,因此不支持並發。 在某些情況下,即使測試應該失敗,測試也會成功通過。

請在此處找到當前狀態以及報告的問題: https://github.com/strethr/testify/issues/187

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM