codecamp

对异常进行测试

对异常进行测试

Example 2.10, “使用 @expectedException 标注”展示了如何用 @expectedException 标注来测试被测代码中是否抛出了异常。

Example 2.10. 使用 @expectedException 标注

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException InvalidArgumentException
     */
    public function testException()
    {
    }
}
?>
phpunit ExceptionTest

PHPUnit 5.0.0 by Sebastian Bergmann and contributors.

F

Time: 0 seconds, Memory: 4.75Mb

There was 1 failure:

1) ExceptionTest::testException
Expected exception InvalidArgumentException

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

另外,你可以将 @expectedExceptionMessage@expectedExceptionMessageRegExp@expectedExceptionCode@expectedException 联合使用,来对异常的讯息与代号进行测试,如Example 2.11, “使用 @expectedExceptionMessage@expectedExceptionMessageRegExp@expectedExceptionCode 标注”所示。

Example 2.11. 使用 @expectedExceptionMessage@expectedExceptionMessageRegExp@expectedExceptionCode 标注

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException        InvalidArgumentException
     * @expectedExceptionMessage Right Message
     */
    public function testExceptionHasRightMessage()
    {
        throw new InvalidArgumentException('Some Message', 10);
    }

    /**
     * @expectedException              InvalidArgumentException
     * @expectedExceptionMessageRegExp #Right.*#
     */
    public function testExceptionMessageMatchesRegExp()
    {
        throw new InvalidArgumentException('Some Message', 10);
    }

    /**
     * @expectedException     InvalidArgumentException
     * @expectedExceptionCode 20
     */
    public function testExceptionHasRightCode()
    {
        throw new InvalidArgumentException('Some Message', 10);
    }
}
?>
phpunit ExceptionTest

PHPUnit 5.0.0 by Sebastian Bergmann and contributors.

FFF

Time: 0 seconds, Memory: 3.00Mb

There were 3 failures:

1) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.

2) ExceptionTest::testExceptionMessageMatchesRegExp
Failed asserting that exception message 'Some Message' matches '#Right.*#'.

3) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.

FAILURES!
Tests: 3, Assertions: 6, Failures: 3.

关于 @expectedExceptionMessage@expectedExceptionMessageRegExp@expectedExceptionCode,分别在the section called “@expectedExceptionMessage”the section called “@expectedExceptionMessageRegExp”the section called “@expectedExceptionCode”有更多相关范例。

此外,还可以用 setExpectedException()setExpectedExceptionRegExp() 方法来设定所预期的异常,如Example 2.12, “预期被测代码将引发异常”所示。

Example 2.12. 预期被测代码将引发异常

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase
{
    public function testException()
    {
        $this->setExpectedException('InvalidArgumentException');
    }

    public function testExceptionHasRightMessage()
    {
        $this->setExpectedException(
          'InvalidArgumentException', 'Right Message'
        );
        throw new InvalidArgumentException('Some Message', 10);
    }

    public function testExceptionMessageMatchesRegExp()
    {
        $this->setExpectedExceptionRegExp(
          'InvalidArgumentException', '/Right.*/', 10
        );
        throw new InvalidArgumentException('The Wrong Message', 10);
    }

    public function testExceptionHasRightCode()
    {
        $this->setExpectedException(
          'InvalidArgumentException', 'Right Message', 20
        );
        throw new InvalidArgumentException('The Right Message', 10);
    }
}
?>
phpunit ExceptionTest

PHPUnit 5.0.0 by Sebastian Bergmann and contributors.

FFFF

Time: 0 seconds, Memory: 3.00Mb

There were 4 failures:

1) ExceptionTest::testException
Expected exception InvalidArgumentException

2) ExceptionTest::testExceptionHasRightMessage
Failed asserting that exception message 'Some Message' contains 'Right Message'.

3) ExceptionTest::testExceptionMessageMatchesRegExp
Failed asserting that exception message 'The Wrong Message' contains '/Right.*/'.

4) ExceptionTest::testExceptionHasRightCode
Failed asserting that expected exception code 20 is equal to 10.

FAILURES!
Tests: 4, Assertions: 8, Failures: 4.

Table 2.1, “用于对异常进行测试的方法 ”中列举了用于对异常进行测试的各种方法。

Table 2.1. 用于对异常进行测试的方法

方法 含义
void setExpectedException(string $exceptionName[, string $exceptionMessage = '', integer $exceptionCode = NULL]) 设定预期的 $exceptionName$exceptionMessage$exceptionCode
void setExpectedExceptionRegExp(string $exceptionName[, string $exceptionMessageRegExp = '', integer $exceptionCode = NULL]) 设定预期的 $exceptionName$exceptionMessageRegExp$exceptionCode
String getExpectedException() 返回预期异常的名称。

可以用 Example 2.13, “另一种对异常进行测试的方法” 中所示方法来对异常进行测试。

Example 2.13. 另一种对异常进行测试的方法

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase {
    public function testException() {
        try {
            // ... 预期会引发异常的代码 ...
        }

        catch (InvalidArgumentException $expected) {
            return;
        }

        $this->fail('预期的异常未出现。');
    }
}
?>

Example 2.13, “另一种对异常进行测试的方法” 中预期会引发异常的代码并没有引发异常时,后面对 fail() 的调用将会中止测试,并通告测试有问题。如果预期的异常出现了,将执行 catch 代码块,测试将会成功结束。

数据供给器
对 PHP 错误进行测试
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

3. 命令行测试执行器

7. 未完成的测试与跳过的测试

12. 测试的其他用途

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }