`go test -run NotExist` 为什么会通过?

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

Why does `go test -run NotExist` pass?

问题

如果我运行以下命令:

go test -run NotExist

响应是 PASS。鉴于我的测试文件中没有包含名为 TestNotExist 的测试,我期望上述命令返回 FAIL。

英文:

If I run the following

go test -run NotExist

The response is PASS. Seeing as my test file does not contain a test called TestNotExist I would expect the command above to return FAIL

答案1

得分: 1

没有-run选项时,go test会运行所有的测试。你可以使用-run选项来不运行所有的测试,来过滤掉排除测试(你可以通过要求非可排除测试的名称与正则表达式模式匹配来实现这一点,但这与讨论的重点无关):

Command go,测试包:

默认情况下,go test不需要参数。它会编译和测试当前目录中包含测试的源代码,并运行这些测试。

测试标志的描述:

-run regexp
只运行与正则表达式匹配的测试和示例。

过滤掉所有测试,使得剩下的测试集合中没有测试需要执行,这是完全“正常”的结果。

当没有测试失败时,被认为测试运行通过。如果没有测试匹配,将不会运行任何测试,也不会有测试失败,因此测试运行将通过。

英文:

Without the -run option go test runs all tests. You use the -run option to not run all tests; to filter out, to exclude tests (and you do this in the form of requiring names of non-excludable tests to match a regexp pattern - but this is irrelevant form the point of discussion):

Command go, Test packages:

> By default, go test needs no arguments. It compiles and tests the package with source in the current directory, including tests, and runs the tests.

Description of testing flags:

> -run regexp
> Run only those tests and examples matching the regular expression.

It is a perfectly "normal" outcome that a filtering filters out all tests, that no tests remains in the set of tests that still need to be executed.

When no tests FAIL, it is considered as the test run PASSes. If no tests match, no tests will run and no tests will FAIL, and thus the test run will PASS.

huangapple
  • 本文由 发表于 2016年7月20日 00:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/38463609.html
匿名

发表评论

匿名网友

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

确定