登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-10 14:30:36 168浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《go测试并行模式下以json格式输出错误的case名称》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

go版本: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: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"}
...

看看这一行 {"time":"2022-06-11t02:48:11.3352891+08:00","action":"output","package":"command-line-arguments","测试":"testp/b","输出":"a 0\n"}.此输出应按 case testp/a 而不是 b 打印,但输出在并行测试中弄乱了 case 名称。

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

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


正确答案


通用 fmt 包对并发环境中当前执行的测试知之甚少,这是有道理的。

测试包有自己的 log 方法,可以正确呈现当前测试:

t.Log("a", i)

今天关于《go测试并行模式下以json格式输出错误的case名称》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>