go testing 在并行模式下以json格式输出错误的case名称

去版本:1.18.1


假设我写了这个测试文件parallel_test.go


package parallel_json_output


import (

    "fmt"

    "testing"

    "time"

)


func TestP(t *testing.T) {

    t.Run("a", func(t *testing.T) {

        t.Parallel()

        for i := 0; i < 5; i++ {

            time.Sleep(time.Second)

            fmt.Println("a", i)

        }

    })

    t.Run("b", func(t *testing.T) {

        t.Parallel()

        for i := 0; i < 5; i++ {

            time.Sleep(time.Second)

            fmt.Println("b", i)

        }

    })

}

跑步后go test parallel_test.go -v -json,我得到了


{"Time":"2022-06-11T02:48:10.3262833+08:00","Action":"run","Package":"command-line-arguments","Test":"TestP"}

{"Time":"2022-06-11T02:48:10.3672856+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP","Output":"=== RUN   TestP\n"}

{"Time":"2022-06-11T02:48:10.3682857+08:00","Action":"run","Package":"command-line-arguments","Test":"TestP/a"}

{"Time":"2022-06-11T02:48:10.3682857+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/a","Output":"=== RUN   TestP/a\n"}

{"Time":"2022-06-11T02:48:10.3692857+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/a","Output":"=== PAUSE TestP/a\n"}

{"Time":"2022-06-11T02:48:10.3702858+08:00","Action":"pause","Package":"command-line-arguments","Test":"TestP/a"}

...

看看这一行{"Time":"2022-06-11T02:48:11.3352891+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"a 0\n"}。此输出应按案例TestP/a而不是打印b,但输出在并行测试中弄乱了案例名称。


这个问题使报告工具生成错误的 HTML 报告,IDE(如 GoLand)也受到影响,无法正确排序并行输出。


我在 Github here中发现了一个问题,但是这个问题似乎已经在 go 1.14.6 中得到修复,但是,它仍然出现在 go 1.18 中。我想知道发生了什么以及如何处理它,非常感谢。


qq_笑_17
浏览 141回答 1
1回答

RISEBY

fmt通用包对并发环境中当前执行的测试知之甚少是有道理的。测试包有自己的Log方法可以正确呈现当前测试:t.Log("a",&nbsp;i)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go