如何在不修改phpunit内容的情况下忽略测试方法?


How can I ignore a test method in phpunit without modifying its content?

为了忽略使用PHPUnit的测试,应该在PHP测试方法旁边放置什么属性?

我知道对于NUnit,属性是:

[Test]
[Ignore]
public void IgnoredTest()

可以使用组注释标记测试,并从运行中排除这些测试。

/**
 * @group ignore
 */
public void ignoredTest() {
    ...
}

然后您可以运行所有测试,但忽略测试,如下所示:

phpunit --exclude-group ignore

最简单的方法就是更改测试方法的名称,避免以"test"开头的名称。这样,除非你告诉PHPUnit使用@test执行它,否则它不会执行那个测试。

同样,您可以告诉PHPUnit跳过特定的测试:

<?php
class ClassTest extends PHPUnit_Framework_TestCase
{     
    public function testThatWontBeExecuted()
    {
        $this->markTestSkipped( 'PHPUnit will skip this test method' );
    }
    public function testThatWillBeExecuted()
    {
        // Test something
    }
}

您可以使用markTestIncomplete()方法来忽略PHPUnit中的测试:

<?php
require_once 'PHPUnit/Framework.php';
class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        // Optional: Test anything here, if you want.
        $this->assertTrue(TRUE, 'This should already work.');
        // Stop here and mark this test as incomplete.
        $this->markTestIncomplete(
            'This test has not been implemented yet.'
        );
    }
}
?>

由于您在您的一个评论中建议您不想更改测试的内容,如果您愿意添加或调整注释,您可以滥用@requires注释来忽略测试:

<?php
use PHPUnit'Framework'TestCase;
class FooTest extends TestCase
{
    /**
     * @requires PHP 9000
     */
    public function testThatShouldBeSkipped()
    {
        $this->assertFalse(true);
    }
}

注意这只会在PHP 9000发布之前工作,并且运行测试的输出也会有点误导:

There was 1 skipped test:
1) FooTest::testThatShouldBeSkipped
PHP >= 9000 is required.

参考:

  • https://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html incomplete-and-skipped-tests.skipping-tests-using-requires

如果你的方法不是以test开头,那么该方法将不会被PHPUnit执行(见这里)。

public function willBeIgnored() {
    ...
}
public function testWillBeExecuted() {
    ...
}

如果你想要执行一个不以test开头的方法,你可以添加注释@test来执行它。

/**
 * @test
 */
public function willBeExecuted() {
    ...
}

扩展@Brady Olsen的回答…,您可以在测试方法或类上使用@group注释,将它们放入任意组中,在本例中为ignore:

/**
 * @group ignore
 */
public function testMethod(): void {
    // ignored...
}

和/或:

/**
 * Every test in this class will be ignored, whether it has a @group or not.
 * @group ignore
 */
class FooTest() {
    public function testBar() {
        // ignored...
    }
}

从这里有几种方法可以排除正在运行的测试。

选项0:通过设置排除

通过将phpunit.xml更改为在根<phpunit>节点下包含<groups>结构来自动排除组:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <!-- ... -->
    </testsuites>
    <groups>
        <exclude>
            <group>ignore</group>
        </exclude>
    </groups>
    <coverage>
        <!-- ... -->
    </coverage>
</phpunit>
同样,ignore文本是任意部分,但它必须匹配@group注释。我敢说,你可以在<exclude>中添加多个<group>元素。

还要注意,<groups>必须立即在<phpunit>节点下,否则如果您的phpunit.xml是畸形的,PHPUnit将尖叫并死亡。

旧版本的PHPUnit使用不同语法的<filter>标记。请看PHPUnit 6.5的答案。为了子孙后代,这个XML配置可以与PHPUnit 9.5.20一起使用。

选项1:通过命令行标志排除

在运行phpunit:

时手动排除组
phpunit --exclude-group ignore