如何在子目录中只测试与给定模式匹配的测试文件?

huangapple go评论115阅读模式
英文:

How to `go test` in sub-directories only tests matching a given pattern?

问题

在我的项目中,我有多个包和子目录。在顶级目录中,我可以运行go test ./...来运行所有子目录中的所有测试。我已经阅读了相当长的时间,了解如何只测试与给定模式或测试名称匹配的测试,但是无法找到解决方案。通过阅读这些(1 2 3godocs),我认为我可以使用以下命令:

  1. go test ./... -run mypattern

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

你可以尝试这样做:

  1. git clone https://github.com/grafana/grafana-plugin-sdk-go.git
  2. > go test -run TestJSON
  3. === RUN TestJSONNotice
  4. === RUN TestJSONNotice/notice_with_severity_and_text
  5. --- PASS: TestJSONNotice (0.00s)
  6. --- PASS: TestJSONNotice/notice_with_severity_and_text (0.00s)
  7. === RUN TestJSON
  8. === RUN TestJSON/json.Unmarshal_and_json.Marshal
  9. === RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error
  10. === RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data
  11. --- PASS: TestJSON (0.00s)
  12. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal (0.00s)
  13. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error (0.00s)
  14. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data (0.00s)
  15. PASS
  16. ok github.com/grafana/grafana-plugin-sdk-go/data 0.016s
  17. > cd data
  18. > go test -run *TestJSON*
  19. zsh: no matches found: *TestJSON*
  20. > go test -run ^TestJSON*
  21. zsh: no matches found: ^TestJSON*
  22. > go test -v -cover --short -race ./... -run ^TestFloatAt*
  23. zsh: no matches found: ^TestFloatAt*

有人能告诉我如何只测试与给定模式或测试名称匹配的测试吗?

英文:

In my project I have multiple packages and sub-directories. In top directory, I can run go test ./... and it runs all the tests in all sub-directories. I have been reading for quite some time on how test only tests matching a given pattern or test name but unable to find a solution. By going through these (1 2 3 and godocs) I thought I could use this:

  1. go test ./... -run mypattern

to run tests matching mypattern but for me it runs all the tests. For now I am using a combination of cd, find and grep to run tests matching my criteria.

You can try this:

  1. git clone https://github.com/grafana/grafana-plugin-sdk-go.git
  2. > go test -run TestJSON
  3. === RUN TestJSONNotice
  4. === RUN TestJSONNotice/notice_with_severity_and_text
  5. --- PASS: TestJSONNotice (0.00s)
  6. --- PASS: TestJSONNotice/notice_with_severity_and_text (0.00s)
  7. === RUN TestJSON
  8. === RUN TestJSON/json.Unmarshal_and_json.Marshal
  9. === RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error
  10. === RUN TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data
  11. --- PASS: TestJSON (0.00s)
  12. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal (0.00s)
  13. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_run_without_error (0.00s)
  14. --- PASS: TestJSON/json.Unmarshal_and_json.Marshal/Should_create_equal_data (0.00s)
  15. PASS
  16. ok github.com/grafana/grafana-plugin-sdk-go/data 0.016s
  17. > cd data
  18. > go test -run *TestJSON*
  19. zsh: no matches found: *TestJSON*
  20. > go test -run ^TestJSON*
  21. zsh: no matches found: ^TestJSON*
  22. > go test -v -cover --short -race ./... -run ^TestFloatAt*
  23. zsh: no matches found: ^TestFloatAt*

Can someone please tell me how test only tests matching a given pattern or test name?

答案1

得分: 3

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

例如:

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

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

  1. ? github.com/user/project/internal/rabbitmq [没有测试文件]
  2. === RUN TestErrorMiddleware
  3. printer.go:54: GET http://localhost:45678/surprise
  4. http服务器已在[::]:45678上启动
  5. {"error":{"code":"EXPECTED","status":418},"level":"warning","msg":"expected: here's tea","time":"2021-08-06T17:25:58+03:00"}
  6. --- PASS: TestErrorMiddleware (0.01秒)
  7. PASS
  8. 覆盖率:语句的54.8%
  9. ok ithub.com/user/project/internal/datadog 3.725 覆盖率:语句的70.6%
  10. ? github.com/user/project/internal/ftp [没有测试文件]
  11. ? github.com/user/project/internal/mongo [没有测试文件]
  12. testing: 警告:没有要运行的测试
  13. PASS
  14. 覆盖率:语句的0.0%
  15. ok github.com/user/project/postgres 3.704 覆盖率:语句的0.0% [没有要运行的测试]
  16. ok github.com/user/project/escrow 7.784 覆盖率:语句的0.0% [没有要运行的测试]
  17. === RUN TestError
  18. --- PASS: TestError (0.00秒)
  19. === RUN TestErrorHandling
  20. --- PASS: TestErrorHandling (0.00秒)
  21. PASS
  22. 覆盖率:语句的70.6%
  23. ...

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

英文:

Passing regular expression that fits name of your test should run matched tests...

for examples:

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

runs different tests that's start with TestError in the project...

  1. ? github.com/user/project/internal/rabbitmq [no test files]
  2. === RUN TestErrorMiddleware
  3. printer.go:54: GET http://localhost:45678/surprise
  4. http server started on [::]:45678
  5. {"error":{"code":"EXPECTED","status":418},"level":"warning","msg":"expected: here's tea","time":"2021-08-06T17:25:58+03:00"}
  6. --- PASS: TestErrorMiddleware (0.01s)
  7. PASS
  8. coverage: 54.8% of statements
  9. ok ithub.com/user/project/internal/datadog 3.725s coverage: 70.6% of statements
  10. ? github.com/user/project/internal/ftp [no test files]
  11. ? github.com/user/project/internal/mongo [no test files]
  12. testing: warning: no tests to run
  13. PASS
  14. coverage: 0.0% of statements
  15. ok github.com/user/project/postgres 3.704s coverage: 0.0% of statements [no tests to run]
  16. ok github.com/user/project/escrow 7.784s coverage: 0.0% of statements [no tests to run]
  17. === RUN TestError
  18. --- PASS: TestError (0.00s)
  19. === RUN TestErrorHandling
  20. --- PASS: TestErrorHandling (0.00s)
  21. PASS
  22. coverage: 70.6% of statements
  23. ...

what you targeting is a name of the test (function name that starts with Test)

huangapple
  • 本文由 发表于 2021年8月6日 22:07:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/68682972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定