为了账号安全,请及时绑定邮箱和手机立即绑定

如何在子目录中“去测试”,只测试与给定模式匹配的测试?

如何在子目录中“去测试”,只测试与给定模式匹配的测试?

Go
Cats萌萌 2022-09-19 14:53:32
在我的项目中,我有多个包和子目录。在顶部目录中,我可以运行,它运行所有子目录中的所有测试。我已经阅读了很长一段时间,关于如何只测试与给定模式或测试名称匹配的测试,但无法找到解决方案。通过浏览这些(1 2 3和godocs),我想我可以使用这个:go test ./...go test ./... -run mypattern运行与 mypattern 匹配的测试,但对我来说,它运行所有测试。现在,我正在使用cd,查找和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)PASSok      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*有人可以告诉我如何只测试与给定模式或测试名称匹配的测试吗?
查看完整描述

1 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

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


例如:


go test -v -cover --short -race  ./... -run ^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)


查看完整回答
反对 回复 2022-09-19
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信