运行 PHPUnit 默认为单个测试套件

Running a single testsuite by default in PHPUnit

我的 PHPUnit 配置文件有两个测试套件,unitsystem。当我 运行 测试 运行 和 vendor/bin/phpunit 时,它 运行 包含两个套件中的所有测试。我可以使用 testsuite 标志定位单个套件:vendor/bin/phpunit --testsuite unit,但我需要将测试 运行ner 配置为 运行 默认情况下仅 unit 套件, 并且 运行 integration 仅当使用测试套件标志专门调用时。

我的配置:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
  <testsuites>
    <testsuite name="unit">
      <directory>tests/Unit</directory>
    </testsuite>
    <testsuite name="integration">
      <directory>tests/Integration</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist>
      <directory suffix=".php">src</directory>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-clover" target="build/clover.xml"/>
  </logging>
</phpunit>

似乎没有办法从 phpunit.xml 文件中列出多个测试套件,但只有 运行 一个。但是,如果您确实对更完整的集成和测试环境有一定的控制权,您可以在其中更准确地配置事物,则可以拥有多个 phpunit 配置文件,并设置一个(或多个)具有更多相关环境的命令行设置参数 --configuration <file> 选项与一个配置,将做更多的事情。这至少可以确保最简单的配置将以最简单的方式 运行。

如果您专门 运行 可以随意调用这两个文件,但可能值得考虑将快速 运行 文件称为默认文件 phpunit.xml,特别命名和扩展的文件为 phpunit.xml.dist如果原始 .xml 不存在 ,.dist 文件将默认自动 运行。另一种选择是将 phpunit.xml.dist 文件放在代码存储库中,然后将其复制到 phpunit.xml 文件,其中包含较少的测试套件,该文件本身并未检入版本控制,仅保留在本地(它也可能在 .gitignore 文件或类似文件中被标记为忽​​略。

PHPUnit 6.1.0 起,现在支持 defaultTestSuite 属性。

看看https://github.com/sebastianbergmann/phpunit/pull/2533

这可以在其他 phpunit 属性中使用,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd"
         backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="tests/bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         defaultTestSuite="unit"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="unit">
            <directory suffix="Test.php">tests/Unit</directory>
        </testsuite>
        <testsuite name="integration">
            <directory suffix="Test.php">tests/Integration</directory>
        </testsuite>
    </testsuites>
</phpunit>

您现在可以 运行 phpunit 而不是 phpunit --testsuite unit

测试套件的名称可能区分大小写,因此请注意这一点。