Testing Exceptions
@expectedException declaration is used to test whether the specified exception in the test code is thrown
Code
1 2 require_once ' phpunit/framework.php ';
3
4 class Exceptiontest extends Phpunit_framework_testcase
5 {
6/* *
7 * @expectedException InvalidArgumentException
8 */
9 Public Function TestException ()
10 {
11}
12}
?>
PHPUnit Exceptiontest
PHPUnit 3.4.2 by Sebastian Bergmann.
F
time:0 seconds
There was 1 failure:
1) testexception (exceptiontest)
Expected Exception InvalidArgumentException
failures!
Tests:1, Assertions:1, failures:1.
Alternatively, you can use Setexpectedexception () to set the expected throw exception
Code
1 2 require_once ' phpunit/framework.php ';
3
4 class Exceptiontest extends Phpunit_framework_testcase
5 {
6 Public Function TestException ()
7 {
8 $this, Setexpectedexception (' invalidargumentexception ');
9}
10}
?>
PHPUnit Exceptiontest
PHPUnit 3.4.2 by Sebastian Bergmann.
F
time:0 seconds
There was 1 failure:
1) testexception (exceptiontest)
Expected Exception InvalidArgumentException
failures!
Tests:1, Assertions:1, failures:1.
Testing PHP Errors
By default, PHPUnit converts error,warning,notice that are triggered in execution into a exception.
Code
1 2 class Expectederrortest extends Phpunit_framework_testcase
3 {
4/* *
5 * @expectedException Phpunit_framework_error
6 */
7 Public Function Testfailinginclude ()
8 {
9 include ' not_existing_file.php ';
10}
11}
?>
PHPUnit Expectederrortest
PHPUnit 3.4.2 by Sebastian Bergmann.
.
time:0 seconds
OK (1 Test, 1 assertion)