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

时间:2013-04-17 13:40:17

标签: php unit-testing symfony nunit phpunit

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

我知道对于NUnit,属性是:

[Test]
[Ignore]
public void IgnoredTest()

5 个答案:

答案 0 :(得分:30)

您可以使用group annotation标记测试,并从运行中排除这些测试。

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

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

phpunit --exclude-group ignore

答案 1 :(得分:16)

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

另外,你可以告诉PHPUnit to skip a specific test

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

答案 2 :(得分: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.'
        );
    }
}
?>

答案 3 :(得分:4)

由于您在其中一条评论中建议您不想更改测试内容,如果您愿意添加或调整注释,则可能会滥用@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.

供参考,见:

答案 4 :(得分:3)

如果您在开头使用test命名方法,那么PHPUnit将不会执行该方法(请参阅here)。

public function willBeIgnored() {
    ...
}

public function testWillBeExecuted() {
    ...
}

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

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