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

31

在PHPUnit中,应该将哪个属性放置在PHP测试方法旁边,以便忽略该测试?

我知道对于NUnit来说,该属性是:

[Test]
[Ignore]
public void IgnoredTest()
6个回答

43

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

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

然后你可以运行所有的测试,但可以像这样忽略某些测试:

phpunit --exclude-group ignore

1
请注意,使用默认的PHPUnit设置,不以test开头的方法(例如testFoo())将不会被执行。 - amphetamachine
1
请注意,在所有版本的PHP中,public void ignoredTest()都是语法错误;正确的语法是(PHP 7+)public function ignoredTest(): void或(PHP <7)public function ignoredTest() - amphetamachine

23

最简单的方法是只需更改测试方法的名称,并避免以“test”开头的名称。这样,除非您使用@test告诉PHPUnit执行它,否则它不会执行该测试。

此外,您还可以告诉PHPUnit跳过特定的测试

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

10
您可以使用PHPUnit中的markTestIncomplete()方法来忽略一个测试:
<?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.'
        );
    }
}
?>

谢谢您的回答,但我不想修改测试内容。只是有时候我想忽略一些测试,然后重新将它们添加到测试流程中。 - Gabriel Diaconescu

5

如果您不想更改测试内容,但愿意添加或调整注释,您可以使用@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.

参考文献:

以上为相关内容,请参考。

5
如果你的方法名称不以 test 开头,那么该方法将不会被 PHPUnit 执行(请参见此处)。
public function willBeIgnored() {
    ...
}

public function testWillBeExecuted() {
    ...
}

如果您希望执行不以test开头的方法,您可以添加注释@test以便无论如何都能执行它。
/**
 * @test
 */
public function willBeExecuted() {
    ...
}

4

对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>节点下立即包含一个<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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接