【问题标题】:How to `go test` in sub-directories only tests matching a given pattern?如何在子目录中“去测试”只测试匹配给定模式的测试?
【发布时间】:2021-10-11 10:19:25
【问题描述】:

在我的项目中,我有多个包和子目录。在顶级目录中,我可以运行go test ./...,它会运行所有子目录中的所有测试。我已经阅读了很长一段时间关于如何仅测试与给定模式或测试名称匹配但无法找到解决方案的内容。通过这些(123godocs)我想我可以使用这个:

go test ./... -run mypattern

运行与 mypattern 匹配的测试,但对我来说它运行所有测试。现在我正在使用 cdfindgrep 的组合来运行符合我的条件的测试。

你可以试试这个:

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 中缺少或没有测试用例。来自节点也没有帮助。

标签: go testing


【解决方案1】:

传递适合您的测试名称的正则表达式应该运行匹配的测试...

例如:

go test -v -cover --short -race  ./... -run ^TestError*

在项目中运行以 TestError 开头的不同测试...

?       github.com/user/project/internal/rabbitmq       [no test files]
=== RUN   TestErrorMiddleware
    printer.go:54: GET http://localhost:45678/surprise
⇨ http server started on [::]:45678
{"error":{"code":"EXPECTED","status":418},"level":"warning","msg":"expected: here's tea","time":"2021-08-06T17:25:58+03:00"}
--- PASS: TestErrorMiddleware (0.01s)
PASS
coverage: 54.8% of statements
ok      ithub.com/user/project/internal/datadog        3.725s  coverage: 70.6% of statements
?       github.com/user/project/internal/ftp    [no test files]
?       github.com/user/project/internal/mongo  [no test files]
testing: warning: no tests to run
PASS
coverage: 0.0% of statements
ok      github.com/user/project/postgres       3.704s  coverage: 0.0% of statements [no tests to run]
ok      github.com/user/project/escrow  7.784s  coverage: 0.0% of statements [no tests to run]
=== RUN   TestError
--- PASS: TestError (0.00s)
=== RUN   TestErrorHandling
--- PASS: TestErrorHandling (0.00s)
PASS
coverage: 70.6% of statements
...

您的目标是测试的名称(以Test 开头的函数名称)

【讨论】:

    猜你喜欢
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2014-09-07
    • 2016-10-07
    • 2022-10-22
    • 1970-01-01
    相关资源
    最近更新 更多