默认情况下在PHPUnit中运行单个测试套件

问题描述 投票:12回答:2

我的PHPUnit配置文件有两个测试套件,unitsystem。当我运行测试运行器vendor/bin/phpunit时,它会在两个套件中运行所有测试。我可以使用testsuite标记来定位单个套件:vendor/bin/phpunit --testsuite unit,但我需要配置测试运行器默认情况下仅运行unit套件,并且仅在使用testsuite标志进行专门调用时才运行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>
php unit-testing phpunit
2个回答
2
投票

似乎没有办法从phpunit.xml文件列出多个测试套件,但只运行一个。但是,如果您确实可以控制更完整的集成和测试环境,您可以更精确地配置事物,那么您可以拥有多个phpunit配置文件,并设置一个(或多个)更复杂的环境来设置命令行参数--configuration <file>选项,配置可以做更多。这至少可以确保最简单的配置以最简单的方式运行。

如果你专门运行它们,可以随意调用这两个文件,但是可能值得考虑将快速运行的文件称为默认的phpunit.xml,将具体命名和扩展的文件作为phpunit.xml.dist。如果原始普通.xml不存在,则默认情况下将自动运行.dist文件。另一个选择是将phpunit.xml.dist文件放在代码存储库中,然后将其复制到phpunit.xml文件中,使用较少的testsuite,它本身不会检入版本控制,并且只保留在本地(它可能也会被标记为忽​​略在.gitignore文件或类似文件中)。


15
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.