PHPUnit:当一个失败时跳过所有测试

mel*_*oon 3 php phpunit

我的测试用例中的所有测试都取决于第一次测试通过.现在我知道我可以将@depends注释添加到其余的测试中,但我正在寻找一种快捷方式来将注释添加到其他所有测试方法中.有没有办法告诉PHPUnit,"如果这个测试失败,请跳过其余的测试"?

Gor*_*don 6

您可以将该特定测试添加到setup()方法中.这将使所有测试失败,例如,跳过此测试

public function setup()
{
    $this->subjectUnderTest = new SubjectUnderTest;
    $this->assertPreconditionOrMarkTestSkipped();
}

public function assertPreconditionOrMarkTestSkipped()
{
    if ($someCondition === false) {
        $this->markTestSkipped('Precondition is not met');
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*ess 6

类似于Gordon的答案,但没有移动第一个测试setUp,类似的方法是在第一个测试中根据其成功/失败设置类级属性并检查此属性setUp.唯一的区别是第一次测试只执行一次,就像现在一样.

private static $firstPassed = null;

public function setup() {
    if (self::$firstPassed === false) {
        self::markTestSkipped();
    }
    else if (self::$firstPassed === null) {
        self::$firstPassed = false;
    }
}

public function testFirst() {
    ... the test ...
    self::$firstPassed = true;
}
Run Code Online (Sandbox Code Playgroud)