亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

go testing 在并行模式下以json格式輸出錯誤的case名稱

go testing 在并行模式下以json格式輸出錯誤的case名稱

Go
qq_笑_17 2022-12-19 21:02:59
去版本:1.18.1假設我寫了這個測試文件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,我得到了{"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 中。我想知道發生了什么以及如何處理它,非常感謝。
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

fmt通用包對并發環境中當前執行的測試知之甚少是有道理的。

測試包有自己的Log方法可以正確呈現當前測試:

t.Log("a", i)


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號