[PHP] PHPUnit entry 3

Source: Internet
Author: User
[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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.