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

Gab*_*scu 21 php phpunit nunit unit-testing symfony

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

我知道对于NUnit,属性是:

[Test]
[Ignore]
public void IgnoredTest()
Run Code Online (Sandbox Code Playgroud)

小智 31

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

/**
 * @group ignore
 */
public void ignoredTest() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

phpunit --exclude-group ignore
Run Code Online (Sandbox Code Playgroud)


Jos*_*sto 16

最简单的方法是只更改测试方法的名称,并避免以"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
    }
}
Run Code Online (Sandbox Code Playgroud)


Vin*_*abu 8

您可以使用该方法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.'
        );
    }
}
?>
Run Code Online (Sandbox Code Playgroud)


m13*_*13r 5

如果您的方法名称test开头不是 with ,那么该方法将不会被 PHPUnit 执行(请参阅此处)。

public function willBeIgnored() {
    ...
}

public function testWillBeExecuted() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

/**
 * @test
 */
public function willBeExecuted() {
    ...
}
Run Code Online (Sandbox Code Playgroud)


loc*_*inz 5

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

<?php

use PHPUnit\Framework\TestCase;

class FooTest extends TestCase
{
    /**
     * @requires PHP 9000
     */
    public function testThatShouldBeSkipped()
    {
        $this->assertFalse(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意这仅在PHP 9000发布之前有效,并且运行测试的输出也会产生误导:

There was 1 skipped test:

1) FooTest::testThatShouldBeSkipped
PHP >= 9000 is required.
Run Code Online (Sandbox Code Playgroud)

供参考,请参阅:

  • +1000应该是公认的答案:) (2认同)