對(duì)異常進(jìn)行測試

2018-02-24 15:41 更新

對(duì)異常進(jìn)行測試

Example?2.10, “使用 @expectedException 標(biāo)注”展示了如何用 @expectedException 標(biāo)注來測試被測代碼中是否拋出了異常。

Example?2.10.?使用 @expectedException 標(biāo)注

<?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 聯(lián)合使用,來對(duì)異常的訊息與代號(hào)進(jìn)行測試,如Example?2.11, “使用 @expectedExceptionMessage、@expectedExceptionMessageRegExp@expectedExceptionCode 標(biāo)注”所示。

Example?2.11.?使用 @expectedExceptionMessage、@expectedExceptionMessageRegExp@expectedExceptionCode 標(biāo)注

<?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.

關(guān)于 @expectedExceptionMessage、@expectedExceptionMessageRegExp@expectedExceptionCode,分別在the section called “@expectedExceptionMessage”、the section called “@expectedExceptionMessageRegExp”the section called “@expectedExceptionCode”有更多相關(guān)范例。

此外,還可以用 setExpectedException()setExpectedExceptionRegExp() 方法來設(shè)定所預(yù)期的異常,如Example?2.12, “預(yù)期被測代碼將引發(fā)異?!?/a>所示。

Example?2.12.?預(yù)期被測代碼將引發(fā)異常

<?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, “用于對(duì)異常進(jìn)行測試的方法 ”中列舉了用于對(duì)異常進(jìn)行測試的各種方法。

Table?2.1.?用于對(duì)異常進(jìn)行測試的方法

方法 含義
void setExpectedException(string $exceptionName[, string $exceptionMessage = '', integer $exceptionCode = NULL]) 設(shè)定預(yù)期的 $exceptionName、$exceptionMessage$exceptionCode。
void setExpectedExceptionRegExp(string $exceptionName[, string $exceptionMessageRegExp = '', integer $exceptionCode = NULL]) 設(shè)定預(yù)期的 $exceptionName、$exceptionMessageRegExp$exceptionCode。
String getExpectedException() 返回預(yù)期異常的名稱。

可以用 Example?2.13, “另一種對(duì)異常進(jìn)行測試的方法” 中所示方法來對(duì)異常進(jìn)行測試。

Example?2.13.?另一種對(duì)異常進(jìn)行測試的方法

<?php
class ExceptionTest extends PHPUnit_Framework_TestCase {
    public function testException() {
        try {
            // ... 預(yù)期會(huì)引發(fā)異常的代碼 ...
        }

        catch (InvalidArgumentException $expected) {
            return;
        }

        $this->fail('預(yù)期的異常未出現(xiàn)。');
    }
}
?>

當(dāng)Example?2.13, “另一種對(duì)異常進(jìn)行測試的方法” 中預(yù)期會(huì)引發(fā)異常的代碼并沒有引發(fā)異常時(shí),后面對(duì) fail() 的調(diào)用將會(huì)中止測試,并通告測試有問題。如果預(yù)期的異常出現(xiàn)了,將執(zhí)行 catch 代碼塊,測試將會(huì)成功結(jié)束。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)