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

Phpunit starts all the tests or not starts all the tests, ignoring the config

我的项目包含 2 个包,我只想 运行 测试其中一个。使用 symfony 3.3phpunit 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>

以及项目结构

此配置将 运行 来自 AppBundle 和 CoreBundle 的所有测试(第二个没有测试),如果您更改

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

<directory>src/CoreBundle/Tests</directory>

那么根本就没有测试。我不明白哪里出了问题

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

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

这是 phpunit 将检查测试的地方。它们必须符合通常的约定,例如文件名以 Test 结尾,并且每个测试方法必须以 test.

为前缀

另外,从您的屏幕截图中我可以了解到您有一个顶级 tests/ 文件夹(紧挨着 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 实际上不包含测试-类,仅包含用于测试的助手。

既然您已经将所有测试都放在一个大的测试文件夹中,这些文件夹由捆绑包分隔,您可能想在此文件夹中搜索 类 扩展 PHPUnit_Framework_TestCase。由于 PHPUnit 6.0 引入了名称空间,因此需要使用 PHPUnit\Framework\TestCase 更新名称空间,否则 PHPUnit 将忽略这些测试。