在并行模式下,Go测试以JSON格式输出错误的用例名称。

huangapple go评论90阅读模式
英文:

go testing outputs wrong case names in json format under parallel mode

问题

你好!以下是你提供的代码的翻译:

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:10.3702858+08:00","Action":"run","Package":"command-line-arguments","Test":"TestP/b"}
{"Time":"2022-06-11T02:48:10.3712858+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"=== RUN   TestP/b\n"}
{"Time":"2022-06-11T02:48:10.3712858+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"=== PAUSE TestP/b\n"}
{"Time":"2022-06-11T02:48:10.3722859+08:00","Action":"pause","Package":"command-line-arguments","Test":"TestP/b"}
{"Time":"2022-06-11T02:48:10.373286+08:00","Action":"cont","Package":"command-line-arguments","Test":"TestP/a"}
{"Time":"2022-06-11T02:48:10.373286+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/a","Output":"=== CONT  TestP/a\n"}
{"Time":"2022-06-11T02:48:10.374286+08:00","Action":"cont","Package":"command-line-arguments","Test":"TestP/b"}
{"Time":"2022-06-11T02:48:10.374286+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"=== CONT  TestP/b\n"}
{"Time":"2022-06-11T02:48:11.3352891+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"b 0\n"}
{"Time":"2022-06-11T02:48:11.3352891+08:00","Action":"output","Package":"command-line-arguments","Test":"TestP/b","Output":"a 0\n"}
...

请注意这一行 {&quot;Time&quot;:&quot;2022-06-11T02:48:11.3352891+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;a 0\n&quot;}。这个输出应该由测试用例 TestP/a 而不是 b 打印出来,但是输出混乱了并行测试中的测试用例名称。

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

我在 GitHub 上找到了这个问题的讨论 链接,但是这个问题似乎在 go 1.14.6 中已经修复了,然而,在 go 1.18 中仍然存在。
我想知道发生了什么以及如何处理这个问题,非常感谢。

英文:

go version: 1.18.1

suppose i wrote this test file parallel_test.go

package parallel_json_output

import (
	&quot;fmt&quot;
	&quot;testing&quot;
	&quot;time&quot;
)

func TestP(t *testing.T) {
	t.Run(&quot;a&quot;, func(t *testing.T) {
		t.Parallel()
		for i := 0; i &lt; 5; i++ {
			time.Sleep(time.Second)
			fmt.Println(&quot;a&quot;, i)
		}
	})
	t.Run(&quot;b&quot;, func(t *testing.T) {
		t.Parallel()
		for i := 0; i &lt; 5; i++ {
			time.Sleep(time.Second)
			fmt.Println(&quot;b&quot;, i)
		}
	})
}

after running go test parallel_test.go -v -json, i got

{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3262833+08:00&quot;,&quot;Action&quot;:&quot;run&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3672856+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP&quot;,&quot;Output&quot;:&quot;=== RUN   TestP\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3682857+08:00&quot;,&quot;Action&quot;:&quot;run&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3682857+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;,&quot;Output&quot;:&quot;=== RUN   TestP/a\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3692857+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;,&quot;Output&quot;:&quot;=== PAUSE TestP/a\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3702858+08:00&quot;,&quot;Action&quot;:&quot;pause&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3702858+08:00&quot;,&quot;Action&quot;:&quot;run&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3712858+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;=== RUN   TestP/b\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3712858+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;=== PAUSE TestP/b\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.3722859+08:00&quot;,&quot;Action&quot;:&quot;pause&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.373286+08:00&quot;,&quot;Action&quot;:&quot;cont&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.373286+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/a&quot;,&quot;Output&quot;:&quot;=== CONT  TestP/a\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.374286+08:00&quot;,&quot;Action&quot;:&quot;cont&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:10.374286+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;=== CONT  TestP/b\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:11.3352891+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;b 0\n&quot;}
{&quot;Time&quot;:&quot;2022-06-11T02:48:11.3352891+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;a 0\n&quot;}
...

look at this line {&quot;Time&quot;:&quot;2022-06-11T02:48:11.3352891+08:00&quot;,&quot;Action&quot;:&quot;output&quot;,&quot;Package&quot;:&quot;command-line-arguments&quot;,&quot;Test&quot;:&quot;TestP/b&quot;,&quot;Output&quot;:&quot;a 0\n&quot;}. this output should be printed by case TestP/a instead of b, but the output messed up the case name in parallel tests.

this problem made reporting tool generate wrong HTML report, IDEs (like GoLand) are effected too and cannot sort parallel output correctly.

i found an issue of it in Github here, but this issue seems had been fixed already in go 1.14.6, however, it still appears in go 1.18.
i wonder what happend and how to deal with it, many thanks.

答案1

得分: 1

在并发环境中,通用的fmt包对当前执行的测试了解很少是有道理的。

测试包有自己的Log方法,可以正确地显示当前的测试:

t.Log("a", i)
英文:

It makes sense that generic fmt package has little knowledge about currently executed tests in concurrent environment.

Testing package has its own Log method that correctly renders current test:

t.Log(&quot;a&quot;, i)

huangapple
  • 本文由 发表于 2022年6月11日 03:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72578829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定