[PHP] PHPUnit Introduction to the third

Source: Internet
Author: User
Tags throw exception
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)
  • 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.