【问题标题】:How to exclude file from PHPUnit test suite in xml config?如何从 xml 配置中的 PHPUnit 测试套件中排除文件?
【发布时间】:2011-02-13 17:55:57
【问题描述】:

我有以下非常简单的 PHPUnit XML 配置:

<phpunit bootstrap="/_tests/TestAutoload.php">
    <testsuites>
        <testsuite name="Unit Tests">
            <directory suffix=".php">_tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

如何从测试套件中排除此目录中的某些文件?我尝试了&lt;exclude&gt;&lt;blacklist&gt;,但在这种情况下似乎不起作用。除了phpunit.de 之外,也找不到任何其他文档,其中没有提及任何相关内容。除此之外,此配置完美运行。

【问题讨论】:

  • 您是否试图避免运行一项或多项测试?
  • 是的,这正是我想要做的。 1 个测试 class= 1 个文件。

标签: unit-testing configuration phpunit


【解决方案1】:

排除文件名TestCase.php

将此添加到您的phpunit.xml

<testsuites>
    <testsuite name="BLABLA">
        <directory suffix=".php">./tests</directory>
        <exclude>./tests/TestCase.php</exclude>
    </testsuite>
</testsuites>

这是来自a real-live test-suite 的额外摘录,我可以确认它正在使用:

...
    <testsuites>
        <testsuite name="n98-magerun-tests">
            <directory>./tests</directory>
            <exclude>tests/N98/Magento/Command/Installer/UninstallCommandTest.php</exclude>
        </testsuite>
    ...

【讨论】:

  • 这对你真的有用吗?根据之前的评论 (stackoverflow.com/a/15632735/2314626) 排除目录作品。您提出的配置仍然失败:在“TestCase”类中找不到测试。
  • @ElanRuusamäe:是的,它确实有效。 先前的评论实际上是多年前的答案,甚至乞求被证明是错误的。
  • 已确认。也许它过去不工作,但它确实适用于 phpunit 5.3,并且是最简单、最清晰的答案。
  • 确认它适用于文件和目录。我正在使用 PHPUnit 9.5.7
  • 在我的情况下,它被排除在外,但对于 Autoloader 也是不可见的。所以扩展 TestCase 的测试只是有错误 can't load the class TestCase
【解决方案2】:

有多种方法可以不运行特定测试 - 将其放入黑名单,使其永远不会运行 - 因为更改它意味着编辑黑名单,而且您经常会在此过程中反复进出版本控制。

还有其他几种可能更合适的方法:

如果测试尚未准备好运行:

$this->markTestIncomplete('This test has not been implemented yet.');

如果有外部原因不应该运行,请跳过它:

if (!extension_loaded('mysqli')) {
    $this->markTestSkipped('The MySQLi extension is not available.');
}

您也可以将其放入setUp() 函数中,这样它将跳过测试类中的所有测试。

您可以根据前一个成功的测试进行测试:

public function testEmpty()
{
    $stack = array();
    $this->assertTrue(empty($stack));
    return $stack;   // also sends this variable to any following tests - if this worked
}
/**
 * only runs if testEmpty() passed
 *
 * @depends testEmpty
 */
public function testPush(array $stack)
{
}

@group -name- 注释是专门停止或运行一组测试的最佳方法之一

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}

testSomething() 现在分为两组,如果在命令行中(或在 config.xml 中添加)--exclude-group 参数。它不会运行。同样,您可以只运行属于特定组的测试 - 例如以功能或错误报告命名。

【讨论】:

  • "@group" 正是我想要的。谢谢!
  • 我希望@group 除了配置中的 phpunit 节点之外,还可以在 testsuite 节点中工作。我有一个测试需要在为所有其他测试设置静态变量之前运行。所以我把那个测试放在一个单独的测试类中,名为 RunFirst.php 并修改了我的配置以在一个套件中单独运行它,但在所有其他套件之前。
  • phpunit.xml中排除组的语法如下&lt;groups&gt;&lt;exclude&gt;&lt;group&gt;ExcludedTests&lt;/group&gt;&lt;/exclude&gt;&lt;/groups&gt;stackoverflow.com/a/49246266/2511355
【解决方案3】:

有了这个 PHPUnit 配置文件,我获得了很好的体验。

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    colors="true"
    processIsolation="true"
    stopOnFailure="true"
    syntaxCheck="false"
    backupGlobals="false"
    bootstrap="test-bootstrap.php">
    <testsuites>
        <testsuite name="php-dba-cache">
          <directory suffix="Test.php">tests</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="coverage-html"
             target="build/coverage"
             charset="UTF-8"
             yui="true"
             highlight="true"
             lowUpperBound="35"
             highLowerBound="70"/>
   </logging>
    <filter>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
            <exclude>
             <file>test-bootstrap.php</file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

https://github.com/gjerokrsteski/php-dba-cache

【讨论】:

  • 您在此配置中的哪个位置排除了目录/文件? 标签用于代码覆盖。
【解决方案4】:

2021 年更新

除了上面的一些好的单一答案之外,还有一个完整的方法,它允许您应用一致、干净、更受架构驱动的测试组织和方便的快速测试工作流程。使用它,您可以从phpunit.xml 管理您的测试、目录和测试套件,并根据需要分组或按一个运行测试。因此,请执行以下操作:

  • 将您的测试分组到目录中(例如tests/Unittests/Featuretests/Integration);
  • 使用&lt;testsuite&gt; 元素使测试套件对您的目录进行分组(注意,在PHPUnit 的更高版本中,您必须在&lt;testsuites&gt; 标记中使用wrap 多个&lt;testsuite&gt; 标记);
  • 创建一个all 测试套件,它结合了您将作为完整测试套件运行的所有默认测试,并通过&lt;phpunit&gt; 元素中的defaultTestSuite="all" 键对其进行分配,如下所示:
  <phpunit .. some other keys
         defaultTestSuite="all">
    <testsuite name="all">
        <directory suffix="Test.php">./tests/Feature/</directory>
        <directory suffix="Test.php">./tests/Unit/</directory>
    </testsuite>   
  </phpunit>
  • 如果您需要制作一个专用的Tinker 测试套件,其中包含可用于修补、保留示例测试等的测试,您可以将其排除在正常的测试工作流程之外。不要将其包含在 all 测试套件中。

所以你将能够:

  • 使用phpunit CLI 命令始终运行默认的all 测试。
  • 使用 CLI 过滤任何测试套件的测试套件、测试文件或单个测试级别,例如:
    • phpunit --testsuite SuiteOne,
    • phpunit --filter SomeTest
    • phpunit --filter SomeTest::test_some_test_method
    • --testsuite--filter 参数结合起来

将此工作流程与在您的 IDE 中运行当前测试或测试文件的能力相结合(对于 Sublime Text,有 MacLinux/Windows 插件),您将完全有能力立即选择要执行的测试。

【讨论】:

  • 感谢phpunit --filter SomeTest::test_some_test_method!这正是我想要的。
【解决方案5】:

当涉及到测试套件中的排除时,phpunit documentation 有点简约。显然,只能排除整个目录,而不能排除单个文件。我会很高兴被证明是错误的。解决方法似乎是使用 @group 功能作为 Alister Bulman 的 posted above

在我想保留的那些测试套件中标记每个测试是一种痛苦。

【讨论】:

    【解决方案6】:

    对于 Phpunit 6.5,excludewhitelist

    <filter>
        <whitelist>
            <directory suffix=".php">src</directory>
            <exclude>
                <directory>src/Migrations</directory>
                <file>src/kernel.php</file>
            </exclude>
        </whitelist>
    </filter>
    

    【讨论】:

      【解决方案7】:

      我遇到了一个测试类,我想扩展它以用于其他测试。 PHPUnit 会发出关于不包含测试的测试文件的警告。简单地在文件abstract 中声明类会导致 PHPunit 安静下来。

      abstract class SetupSomeCommonThingsTestCase {
          protected function setUp(): void {
              parent:setUp();
              ...
          }
      }
      

      【讨论】:

        【解决方案8】:

        您好,请确保将您的排除项放入白名单。示例:

        <phpunit>
            <filter>
                <blacklist>
                    <directory suffix=".php">/not/even/looked/at/</directory>
                </blacklist>
                <whitelist>
                    <directory suffix=".php">/path/to/test/dir/</directory>
                    <exclude>
                        <file suffix=".php">/path/to/fileToExclude.php</file>
                    </exclude>
                </whitelist>
            </filter>
        </phpunit>
        

        http://www.phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.blacklist-whitelist

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-27
          • 2011-07-27
          • 2011-04-09
          • 2018-10-17
          • 2021-10-02
          • 1970-01-01
          • 1970-01-01
          • 2013-10-03
          相关资源
          最近更新 更多