PHPUnit测试套件排除

20

因此,我希望像这样从我的测试套件中排除一个目录:

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


这个之前的“解决方案”怎么样?https://dev59.com/SXE85IYBdhLWcg3whD7k - achedeuzot
2个回答

19

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

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

当在其他地方运行并使用 --config 指定配置文件时,exclude 将停止工作。

其次,当路径中有任何通配符(*)时,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指令。

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

  

[...]此修复将在4.0发布中发布,因为它会破坏向后兼容性。

  • 解决方案#1:在路径中删除任何通配符
  • 解决方案#2:升级到PHPUnit v4.*(未经过本人测试,请参阅评论,不能解决带有通配符的exclude路径的问题)

我不知道怎么做。即使我尝试以下操作,它仍将运行目录<directory>../src//Bundle//Bundle/Tests</directory>中的所有测试。 <exclude>../src//Bundle/*/*Bundle/Tests</exclude>你是否遇到了同样的问题?有关PHPUnit版本,请参见更新的问题。 - Tim Joseph
我刚刚测试了一下,发现问题跟你的 完全 一样。请查看回答以获取更多详细信息。 - achedeuzot
非常感谢。我已经在Github上找到了这个问题,但也许是我太蠢或者我的英语太差了,现在该怎么办呢?我一点头绪都没有...或者解决方案是更新到4.0.*吗? - Tim Joseph
我更新了我的答案,我发现唯一的解决方案是从“exclude”指令中删除所有的globbing... 如果你不能升级到4.0.*版本,这不是一个非常有效的解决方案,但我目前没有看到更好的解决方案。 - achedeuzot
我取消了已接受的答案标签,因为在我的版本4.0.12中仍然无法正常工作。 - Tim Joseph
好的,我又查了一些资料,但是我没找到关于排除和通配符的任何内容。所以,如果你真的想/需要使用通配符,我的最后一个建议就是修补PHPUnit文件。 - achedeuzot

8

我刚遇到了类似的问题,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注释?太傻了。 - Szczepan Hołyszewski
这只是一个功能,取决于您如何使用它。 - lchachurski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接