对异常进行测试

2018-02-24 15:41 更新

对异常进行测试

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 代码块,测试将会成功结束。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号