猿问

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

在我的项目中,我有多个包和子目录。在顶部目录中,我可以运行,它运行所有子目录中的所有测试。我已经阅读了很长一段时间,关于如何只测试与给定模式或测试名称匹配的测试,但无法找到解决方案。通过浏览这些(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)

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*

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


Cats萌萌
浏览 110回答 1
1回答

互换的青春

传递符合测试名称的正则表达式应运行匹配的测试...例如: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)PASScoverage: 54.8% of statementsok      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 runPASScoverage: 0.0% of statementsok      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)PASScoverage: 70.6% of statements...目标对象是测试的名称(函数名称以Test)
随时随地看视频慕课网APP

相关分类

Go
我要回答