【问题标题】:Running a single testsuite by default in PHPUnit在 PHPUnit 中默认运行单个测试套件
【发布时间】:2016-10-15 16:03:12
【问题描述】:

我的 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>

【问题讨论】:

  • 创建phpunit_unit.shphpunit_integration.sh 文件,里面有配置不是更好吗?
  • 是否找到了相反的答案?不是默认运行一些测试的方法 - 例如测试升级脚本的大量长期运行的测试?所以必须包括哪个。
  • 我稍后可能会对此有一个关于我通常更喜欢的“替代”方式的答案......答案(甚至是接受的那个)非常复杂。认为重要吗?还是不值得打字?

标签: php unit-testing phpunit


【解决方案1】:

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

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

【讨论】:

  • 在撰写本文时(2019 年 7 月) - 这将需要很长时间才能过滤到大多数用户,最好至少使用组和自述文件或运行其他内容测试(并检测它是否未被使用,改为说“运行[复制和粘贴的东西]”)虽然我不知道有多大的用户群在它上面我检查了 3 个“流行”的东西是 3.7.something (28?) - 显然内部用户会知道那里的目标。但是,如果可能的话,暂时不要使用它
【解决方案2】:

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

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

【讨论】:

  • PHPUnit(自 6.1.0 起)现在支持定义默认测试套件,因此不再需要此解决方法。
  • @GaryJ:链接(至少)到此链接的来源将非常适合超文本。但是非常感谢您的评论和版本号 :) /E: 哎呀,刚刚看到你的 answer below
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2016-11-24
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多