PHPUnit 断言抛出异常? [英] PHPUnit assert that an exception was thrown?
本文介绍了PHPUnit 断言抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道有没有assert
之类的东西可以测试被测代码是否抛出异常?
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?
推荐答案
<?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);
}
}
PHPUnit 作者文章 提供了关于测试异常最好的详细解释实践.
PHPUnit author article provides detailed explanation on testing exceptions best practices.
这篇关于PHPUnit 断言抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文