去版本:1.18.1假設(shè)我寫了這個測試文件parallel_test.gopackage parallel_json_outputimport ( "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,我得到了{(lán)"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"}。此輸出應(yīng)按案例TestP/a而不是打印b,但輸出在并行測試中弄亂了案例名稱。這個問題使報告工具生成錯誤的 HTML 報告,IDE(如 GoLand)也受到影響,無法正確排序并行輸出。我在 Github here中發(fā)現(xiàn)了一個問題,但是這個問題似乎已經(jīng)在 go 1.14.6 中得到修復(fù),但是,它仍然出現(xiàn)在 go 1.18 中。我想知道發(fā)生了什么以及如何處理它,非常感謝。
1 回答

RISEBY
TA貢獻(xiàn)1856條經(jīng)驗 獲得超5個贊
fmt
通用包對并發(fā)環(huán)境中當(dāng)前執(zhí)行的測試知之甚少是有道理的。
測試包有自己的Log
方法可以正確呈現(xiàn)當(dāng)前測試:
t.Log("a", i)
- 1 回答
- 0 關(guān)注
- 133 瀏覽
添加回答
舉報
0/150
提交
取消