让 PHPUnit 忽略东西?

2022-01-25 00:00:00 unit-testing php phpunit

我有一个 PHPUnit 测试类,我想在测试运行中忽略它.我知道我可以通过重命名它以使其文件名中不包含单词 Test 来做到这一点,但我宁愿不这样做,因为它比我想要的更混淆了源代码控制水域.

I have a PHPUnit Test class that I'd like to be ignored from a test run. I know I can do it by renaming it so that it doesn't contain the word Test in its filename, but I'd rather not do that since it muddies up the source control waters more than I'd like.

有人有什么建议吗?

推荐答案

phpunit 命令有几个选项可以帮助定义应该和不应该运行哪些测试:

There are a couple of options for the phpunit command that can help define which tests should and should not be run :

$ phpunit --help
PHPUnit 3.4.0beta5 by Sebastian Bergmann.

Usage: phpunit [switches] UnitTest [UnitTest.php]
       phpunit [switches] <directory>
...
  --filter <pattern>       Filter which tests to run.
  --group ...              Only runs tests from the specified group(s).
  --exclude-group ...      Exclude tests from the specified group(s).
  --list-groups            List available test groups.
...

那些可能不允许指定确切你想要什么......但他们可能会有所帮助.

Those will probably not allow to specify exactly what you wanted... But they might help.

有关详细信息,请参阅 命令行测试运行程序

For more details, see The Command-Line Test Runner


特别是,我有点喜欢组功能:只需在测试的 phpdoc 中使用 @group 标记,将它们重新分组 (例如,每个功能块"一组) ;之后,您可以在命令行上使用 --group--exclude-group 选项来指定应该或不应该运行哪些测试.


Especially, I kind of like the group feature : just use a @group tag in the phpdoc of your tests, to re-group them (for instance, one group per "chunk of functionnality") ; and, after, you can use the --group or --exclude-group options on the command line to specify which tests should or shouldn't be run.

在你的情况下,如果你不能修改测试,也许 --filter 选项会有所帮助,这取决于你的测试是如何命名的(即,如果有一种方法可以识别你想要运行的那些):

In your case, if you cannot modify the tests, maybe the --filter option can help, depending on how your tests are nammed (ie, if there is a way to identify those you want to run) :

--过滤器
仅运行名称与给定模式匹配的测试.图案可以是单个测试的名称或匹配的正则表达式多个测试名称.

--filter
Only runs tests whose name matches the given pattern. The pattern can be either the name of a single test or a regular expression that matches multiple test names.


如果您不总是更改要运行的测试组",另一种解决方案可能是使用仅包含您要运行的测试的测试套件.


Another solution, if you are not always changing the "groups" of tests to run, might be to use a test-suite that only includes the tests you want to run.

例如,看看 使用 XML 配置编写测试套件.

相关文章