【问题标题】:PHPUnit TestSuite ExcludePHPUnit TestSuite 排除
【发布时间】:2023-04-01 04:31:01
【问题描述】:

所以我想从我的测试套件中排除一个目录,就像这样:

<testsuite name="PHPUnitWillKillMe">   
    <directory>src/*/Bundle/*/*Bundle/Tests</directory>
    <exclude>src/*/Bundle/*/*Bundle/Tests/Controller/</exclude>
</testsuite>   

除了控制器之外的所有东西都应该被测试。

问题是,它不起作用。当我运行时,PHPUnit 仍然运行 src//Bundle//*Bundle/Tests/Controller/ 中的所有测试

 phpunit --testsuite PHPUnitWillKillMe

有什么想法吗?

最好的问候!

我测试的 PHPUnit 版本是 3.7.21 和 3.7.28。

【问题讨论】:

标签: php unit-testing phpunit


【解决方案1】:

我在我的 Symfony 演示项目中对其进行了测试(Bundles 表明这是您正在使用的),我也遇到了同样的问题。这似乎是两个问题的结合。首先,使用-c--config 选项运行PHPUnit (PHPUnit 3.7.19) 存在一个已知错误:

https://github.com/sebastianbergmann/phpunit/issues/928

当在别处运行它并使用 --config 指定配置文件时,排除将停止工作。

其次,当路径中有任何通配符 (*) 时,exclude 指令似乎会忽略/失败,因此通过删除通配符,它​​对我有用:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
        <exclude>../src/Blah/MyBundle/Tests/Controller/</exclude>
    </testsuite>
</testsuites>

这是我发现根据需要排除 MyBundle 中的测试的唯一方式。对于exclude,通配符工作。但是,这意味着您必须添加尽可能多的 exclude 指令,因为您要忽略的文件夹数量。

可能相关的 gihub 问题: https://github.com/sebastianbergmann/phpunit/pull/573

[...] 此修复程序在 4.0 版本中出现,因为它破坏了向后兼容性。

  • 解决方案 #1:删除路径中的任何 globbing
  • 解决方案 #2:升级到 PHPUnit v4.*(未经我自己测试,请参阅 cmets,不能解决 exclude 通配路径的问题)

【讨论】:

  • 我不知道怎么做。我什至可以尝试以下操作,它仍然会运行目录 ../src/*/Bundle/*/*Bundle/Tests../src/*/Bundle/ 中的所有测试*/*Bundle/Tests 您是否遇到同样的问题?对于 PHPUnit 版本,请参阅更新后的问题
  • 我刚刚测试过,我有 exact 同样的问题。更多详情见答案
  • 非常感谢。我已经在github上发现了这个问题,但也许我很愚蠢或者我的英语不好,但是现在有什么解决方案?我不知道......或者解决方案可能是更新到 4.0.*?
  • 我更新了我的答案,我发现的唯一解决方案是从exclude 指令中删除所有通配符...如果您无法升级到版本 4.0,这不是一个非常有效的解决方案。 * 但我目前没有看到更好的解决方案。
  • 我删除了接受的答案标签,因为这仍然不适用于我的 4.0.12 版
【解决方案2】:

刚刚遇到类似的问题,phpunit 对群组的支持非常好:

...
--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.
...

你所做的是

/**
 * @group nonRunnableGroupName
 */
public function testSomething()
{ /* test here */ }

然后像这样运行测试

$ phpunit -c /src --exclude-group nonRunnableGroupName

【讨论】:

  • 编辑我需要排除的每个类并将@group 注释添加到每个此类中的每个测试方法?愚蠢。
  • 这只是一个功能,取决于你如何使用它。
猜你喜欢
  • 1970-01-01
  • 2011-11-18
  • 2018-10-31
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 2012-03-30
相关资源
最近更新 更多