英文:
How to `go test` in sub-directories only tests matching a given pattern?
问题
在我的项目中,我有多个包和子目录。在顶级目录中,我可以运行go test ./...
来运行所有子目录中的所有测试。我已经阅读了相当长的时间,了解如何只测试与给定模式或测试名称匹配的测试,但是无法找到解决方案。通过阅读这些(1 2 3和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*
有人能告诉我如何只测试与给定模式或测试名称匹配的测试吗?
英文:
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:
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:
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*
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开头的不同测试...
? github.com/user/project/internal/rabbitmq [没有测试文件]
=== RUN TestErrorMiddleware
printer.go:54: GET http://localhost:45678/surprise
⇨ http服务器已在[::]:45678上启动
{"error":{"code":"EXPECTED","status":418},"level":"warning","msg":"expected: here's tea","time":"2021-08-06T17:25:58+03:00"}
--- PASS: TestErrorMiddleware (0.01秒)
PASS
覆盖率:语句的54.8%
ok ithub.com/user/project/internal/datadog 3.725秒 覆盖率:语句的70.6%
? github.com/user/project/internal/ftp [没有测试文件]
? github.com/user/project/internal/mongo [没有测试文件]
testing: 警告:没有要运行的测试
PASS
覆盖率:语句的0.0%
ok github.com/user/project/postgres 3.704秒 覆盖率:语句的0.0% [没有要运行的测试]
ok github.com/user/project/escrow 7.784秒 覆盖率:语句的0.0% [没有要运行的测试]
=== RUN TestError
--- PASS: TestError (0.00秒)
=== RUN TestErrorHandling
--- PASS: TestErrorHandling (0.00秒)
PASS
覆盖率:语句的70.6%
...
您的目标是测试的名称(以Test
开头的函数名称)
英文:
Passing regular expression that fits name of your test should run matched tests...
for examples:
go test -v -cover --short -race ./... -run ^TestError*
runs different tests that's start with TestError in the project...
? 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
...
what you targeting is a name of the test (function name that starts with Test
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论