Phpunit启动所有测试或不启动所有测试,忽略配置

时间:2017-08-16 12:52:29

标签: phpunit symfony-3.3

我的项目包含2个包,我想只在其中一个包中运行测试。使用 symfony 3.3 phpunit 6.3.0

phpunit.xml.dist

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="./src/CoreBundle/Tests/autoloadWithIsolatedDatabase.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_CLASS" value="AppKernel" />
    </php>

    <testsuites>
        <testsuite name="App">
            <directory>src/AppBundle/Tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*Bundle/Tests</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Tests</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Tests</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

项目结构

structure

这个配置将运行AppBundle和CoreBundle的所有测试(第二个没有测试),如果你改变了

<directory>src/AppBundle/Tests</directory> 

<directory>src/CoreBundle/Tests</directory>
然后根本就没有测试。我无法理解错误的原因

1 个答案:

答案 0 :(得分:1)

让我们从phpunit.xml.dist的配置开始。您定义了一个测试套件:

<testsuites>
    <testsuite name="App">
        <directory>src/AppBundle/Tests</directory>
    </testsuite>
</testsuites>

这是phpunit将考虑进行测试的地方。它们必须符合通常的约定,例如文件名以Test结尾,并且每个测试方法都必须以test为前缀。

同样从你的截图中我可以收集到你有一个顶级测试/文件夹(就在app /,src /等旁边)。这可能是您放置其他测试的地方。

如果您遵循最佳做法,您还应该从AppBundle放置测试的第二个文件夹:https://symfony.com/doc/current/best_practices/tests.html

我认为这是在3.x发布周期的某个时间建立的。

理论上你应该能够将src / AppBundle / Tests复制到tests / AppBundle,希望一切都能正常工作。现在,您可以将测试套件配置更新为:

<testsuites>
    <testsuite name="App">
        <directory>tests/</directory>
    </testsuite>
</testsuites>

您的过滤器可以保留在原位,因为src / CoreBundle / Tests实际上不包含测试类,只有用于测试的帮助程序。

现在您已将所有测试放在一个由bundle分隔的大型测试文件夹中,您可能希望在此文件夹中搜索扩展PHPUnit_Framework_TestCase的类。由于PHPUnit 6.0引入了名称空间,因此需要使用PHPUnit\Framework\TestCase进行更新,否则PHPUnit将忽略这些测试。

相关问题