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

Ond*_*ták 37 configuration phpunit unit-testing

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

<phpunit bootstrap="/_tests/TestAutoload.php">
    <testsuites>
        <testsuite name="Unit Tests">
            <directory suffix=".php">_tests</directory>
        </testsuite>
    </testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

如何从测试套件中排除此目录中的某些文件?我试过<exclude><blacklist>,但它似乎并没有在这方面的工作.还找不到除phpunit.de之外的任何其他文档,其中没有提及任何相关内容.除此之外,此配置完美无缺.

Ali*_*man 37

有许多方法可以不运行特定测试 - 将其置于黑名单中以便它永远不会运行 - 因为更改它意味着编辑黑名单,并且您通常会最终将其反弹并进出版本控制.

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

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

$this->markTestIncomplete('This test has not been implemented yet.');
Run Code Online (Sandbox Code Playgroud)

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

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

您也可以将它放入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)
{
}
Run Code Online (Sandbox Code Playgroud)

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

/**
 * @group database
 * @group remoteTasks
 */
public function testSomething()
{
}
Run Code Online (Sandbox Code Playgroud)

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


Mah*_*alt 33

排除文件名TestCase.php.

将此添加到您的 phpunit.xml

<testsuites>
    <testsuite name="BLABLA">
        <directory suffix=".php">./tests</directory>
        <exclude>./tests/TestCase.php</exclude>
    </testsuite>
</testsuites>
Run Code Online (Sandbox Code Playgroud)

这是一个真实的测试套件的额外摘录,我可以确认它可以使用:

...
    <testsuites>
        <testsuite name="n98-magerun-tests">
            <directory>./tests</directory>
            <exclude>tests/N98/Magento/Command/Installer/UninstallCommandTest.php</exclude>
        </testsuite>
    ...
Run Code Online (Sandbox Code Playgroud)


小智 8

Whit这个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>
Run Code Online (Sandbox Code Playgroud)

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

  • 在这个配置中你在哪里排除dirs /文件?<filter>标签用于代码覆盖. (7认同)

Val*_*Shi 6

2021年更新

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

  • 将您的测试分组到目录中(例如tests/Unit, tests/Feature, tests/Integration);
  • 使测试套件使用元素对目录进行分组<testsuite>(请注意,在 PHPUnit 的更高版本中,您必须将多个标签包装在tag 中);<testsuite><testsuites>
  • 创建一个all测试套件,将您将作为完整测试套件运行的所有默认测试结合起来,并通过元素defaultTestSuite="all"内的键分配它<phpunit>,如下所示:
  <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>
Run Code Online (Sandbox Code Playgroud)
  • 如果您需要制作一个专用的Tinker测试套件,其中包含可用于修补、保留示例测试等的测试,您可以将其排除在正常的测试工作流程之外。不要将其包含在all测试套件中。

所以你将能够:

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

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