【问题标题】:How to set path for go test in vscode如何在 vscode 中设置 go test 的路径
【发布时间】:2022-02-01 13:10:16
【问题描述】:

我有以下源代码树:

/main.go
/templates/t.tmpl
/pkg/templates.go
/pkg/templates_test.go

/pkg/templates.go 定义使用 var t = template.Must(template.ParseFiles("templates/t.tmpl") 这是正常项目操作所必需的,因为二进制文件是从根文件夹调用的。

然后当我从 templates_test.go 调试来自 vscode 的测试时,Must 失败,因为 go test 当前目录是 pkg 而不是项目根目录

如何使用go test制作vscode以使用根项目文件夹?

【问题讨论】:

    标签: unit-testing go testing visual-studio-code vscode-settings


    【解决方案1】:

    从包的源代码目录运行测试是 Go 的默认行为。

    https://pkg.go.dev/cmd/go#hdr-Testing_flags:

    当 'go test' 运行测试二进制文件时,它在 对应包的源码目录。根据测试, 在调用生成的测试时可能需要这样做 直接二进制。

    要改变行为,要么使用相对路径(例如go project's template package test),要么在测试程序开始时更改您想要的目录(例如来自TestMain的os.Chdir等)。

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2020-07-21
      • 2018-07-31
      相关资源
      最近更新 更多