[PHP] PHPUnit entry 3 Testing Exceptions
@ ExpectedException the declaration is used to test whether an exception is thrown in the test code.
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}
13?>
Phpunit predictiontest
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.
You can also use setExpectedException () to set the expected 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}
11?>
Phpunit predictiontest
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 the error, warning, and notice triggered during execution into an 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}
12?>
Phpunit ExpectedErrorTest
PHPUnit 3.4.2 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test, 1 assertion)