如何从xml config中的PHPUnit测试套件中排除文件?

时间:2010-04-29 10:00:55

标签: unit-testing configuration phpunit

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

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

如何从测试套件中排除此目录中的某些文件?我尝试了<exclude><blacklist>,但它似乎不适用于此上下文。还找不到除phpunit.de之外的任何其他文档,其中没有提及任何相关内容。除此之外,这个配置完美无缺。

8 个答案:

答案 0 :(得分:35)

有很多方法可以不运行特定的测试 - 将其放入黑名单,因此它永远不会运行 - 因为更改它意味着编辑黑名单,并且您经常会最终将其弹出和弹出版本控制。

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

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

$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参数中添加了任一组。它不会被运行。同样,您只能运行属于特定组的测试 - 例如以功能或错误报告命名。

答案 1 :(得分:29)

排除文件名Child1

将此添加到您的TestCase.php

phpunit.xml

以下是a real-live test-suite的其他摘录,我可以确认它与之合作:

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

答案 2 :(得分:8)

使用这个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

答案 3 :(得分:4)

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

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

答案 4 :(得分:2)

通过上述解决方案,您可以使用更多由体系结构驱动的测试工作流,您可以在其中管理phpunit.xml中的测试,目录和测试套件,如下所示:

  • 将测试分组到目录中(例如tests/Unittests/Feature);
  • 根据需要进行测试,使用<testsuite>元素对目录进行分组;
  • 创建一个All测试套件,它将您将作为完整测试套件运行的所有默认测试组合在一起,并通过<phpunit>元素中的defaultTestSuite="All"键进行分配。
  • 制作一个专用的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插件),您将完全有能力立即选择测试执行。

答案 5 :(得分:1)

对于Phpunit 6.5,excludewhitelist

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

答案 6 :(得分:0)

我在一个测试类中遇到了这个问题,我想为其他测试扩展它。 PHPUnit 会发出有关不包含测试的测试文件的警告。只需在文件 abstract 中声明类,PHPunit 就会对此保持沉默。

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

答案 7 :(得分:-4)

嘿那里,请确保将您的排除项放在白名单中。例如:

<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

相关问题