duanjiao3686 2018-09-19 16:56
浏览 13
已采纳

报告测试失败时,为什么golang测试包会用下划线替换空格?

When I run this test and it fails:

func TestCaseA(t *testing.T){
    t.Run("my name with spaces", func (t *testing.T){
        t.Error("some error message")
    })
}

then the name of the test is modified (spaces are changed to underscores) in the output:

--- FAIL: TestCaseA (0.00s)
    --- FAIL: TestCaseA/my_name_with_spaces (0.00s)
        main.go:10: some error message
FAIL

Why does it do this?

Here's a working example: https://play.golang.org/p/viZjC60Dazg

  • 写回答

2条回答 默认 最新

  • doufu5521 2018-09-19 19:29
    关注

    It's explained in this blog post: https://blog.golang.org/subtests

    The full name of a subtest or sub-benchmark is a slash-separated list of its name and the names of all of its parents, starting with the top-level. The name is the corresponding function name for top-level tests and benchmarks, and the first argument to Run otherwise. To avoid display and parsing issues, a name is sanitized by replacing spaces with underscores and escaping non-printable characters. The same sanitizing is applied to the regular expressions passed to the -run or -bench flags.

    Some examples could be to make it simpler for automated tools to process the test output, to avoid issues with the shell making regex arguments containing spaces appearing as multiple arguments etc. I'd rather avoid using spaces than deal with this magic renaming.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器