【发布时间】:2021-10-11 10:19:25
【问题描述】:
在我的项目中,我有多个包和子目录。在顶级目录中,我可以运行go test ./...,它会运行所有子目录中的所有测试。我已经阅读了很长一段时间关于如何仅测试与给定模式或测试名称匹配但无法找到解决方案的内容。通过这些(123和godocs)我想我可以使用这个:
go test ./... -run mypattern
运行与 mypattern 匹配的测试,但对我来说它运行所有测试。现在我正在使用 cd、find 和 grep 的组合来运行符合我的条件的测试。
你可以试试这个:
git clone https://github.com/grafana/grafana-plugin-sdk-go.git
> go test -run TestJSON
=== RUN TestJSONNotice
=== RUN TestJSONNotice/notice_with_severity_and_text
--- PASS: TestJSONNotice (0.00s)
--- PASS: TestJSONNotice/notice_with_severity_and_text (0.00s)
=== RUN TestJSON
=== RUN TestJSON/json.Unmarshal_and_json.Marshal
=== RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error
=== RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data
--- PASS: TestJSON (0.00s)
--- PASS: TestJSON/json.Unmarshal_and_json.Marshal (0.00s)
--- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error (0.00s)
--- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data (0.00s)
PASS
ok github.com/grafana/grafana-plugin-sdk-go/data 0.016s
> cd data
> go test -run *TestJSON*
zsh: no matches found: *TestJSON*
> go test -run ^TestJSON*
zsh: no matches found: ^TestJSON*
> go test -v -cover --short -race ./... -run ^TestFloatAt*
zsh: no matches found: ^TestFloatAt*
谁能告诉我如何只测试匹配给定模式或测试名称的测试?
【问题讨论】:
-
它确实有效(尽管我建议不要在参数后放置标志,因为大多数 go 程序不解析它们)。请出示 minimal reproducible example 来证明您遇到的问题。
-
通过测试不会打印任何内容,您真的确定它们没有正在运行吗?
-
它似乎在我的机器上运行良好。你能分享“mypattern”的确切值吗?你能在公共 GitHub 存储库中重现它吗?
-
@Raaka 您需要将表达式放在引号中,您的 shell 正在尝试将其解析为文件通配符,您可以知道这是因为您收到的是 shell 错误而不是 go test 错误。
-
感谢@Adrian,这就是我今天开始新一天的方式:10094 8/6/2021 09:07 go test -run "^TestLoad$" 这是不使用 -v 标志和引号的组合稍后一点。 go 的输出也没有帮助,它对遗漏的测试用例说“[没有要运行的测试]”。由于我是我正在处理的 repo 的新手,我认为这是由于 repo 中缺少或没有测试用例。来自节点也没有帮助。