Does anyone know whether there is an assert
or something like that which can test whether an exception was thrown in the code being tested?
PHPUnit断言抛出了异常?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
13条回答 默认 最新
- dongxie9169 2011-04-16 00:26关注
<?php require_once 'PHPUnit/Framework.php'; class ExceptionTest extends PHPUnit_Framework_TestCase { public function testException() { $this->expectException(InvalidArgumentException::class); // or for PHPUnit < 5.2 // $this->setExpectedException(InvalidArgumentException::class); //...and then add your test code that generates the exception exampleMethod($anInvalidArgument); } }
expectException() PHPUnit documentation
PHPUnit author article provides detailed explanation on testing exceptions best practices.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报