如何在未生成预期异常时测试PHPUnit_Framework_TestCase是否失败

时间:2015-10-19 15:18:39

标签: unit-testing exception phpunit

我正在尝试扩展PHPUnit的功能。

我想扩展它关于如何允许测试断言预期Exception被抛出的行为。

我通过扩展PHPUnit_Framework_TestCase完成了这项工作,如下所示:

class Improved_TestCase extends PHPUnit_Framework_TestCase {
    //...
}

问题是,我现在需要编写一个测试,证明如果没有抛出@expectedException,测试将会失败。

例如,我想表明以下测试会失败:

/**
 * I want to write a TestTest that passes when these tests would fail
 */
class FooTest extends Improved_TestCase {

    /**
     * @expectedException Exception
     */
    function testFooThrowsAnnotatedException()
    {
        Foo::thisWillNotThrowAnException();
    }

    function testFooThrowsSetException()
    {
        $this->setExpectedException('Exception');

        Foo::thisWillNotThrowAnException();
    }

}

上述测试应该失败。但有些人我需要编写一个测试,当上述测试失败时会通过

我已尝试查看PHPUnit's tests source code,但我不了解它如何或在何处测试预期的异常测试的工作情况。

据我所知,我最好的猜测是@expectedException behaviour is tested here,但它没有意义,因为如果PHPUnit工作正常,那些测试会失败!< / p>

1 个答案:

答案 0 :(得分:0)

测试它的代码是here in TestCaseTest.php

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>T</title>
</head><body>noaeuvpraoisevuapnrsoievunapsoirevuanpsiorvunapeisovrnupaisovurapiosevraes<br></body></html>