Use unit tests to check PHP code on each layer (1) _php tutorial

Source: Internet
Author: User
Tags pear
The Web application runs 24x7, so my program is still running this problem will haunt me at night. Unit tests have helped me build up enough confidence in my code-so I can sleep soundly.

Unit testing is a framework for writing test cases for code and automatically running those tests. Test-driven development is a unit-testing approach, and the idea is that you should write the test program first, and verify that the tests can find errors before you start writing code that needs to pass these tests. When all the tests passed, the features we developed were done. The value of these unit tests is that we can run them at any time-before the code is checked in, after significant modifications, or after being deployed to a running system.

PHP Unit Test

For PHP, the Unit test framework is PHPUnit2. You can use the Pear command line as a pear module to install the system:% pear Install PHPUnit2.

After you install this framework, you can write unit tests by creating a test class that is derived from phpunit2_framework_testcase.

Module Unit Test

I found that the best place to start unit testing was in the business logic module of the application. I used a simple example: this is a function to sum two numbers. To start the test, we first write the test case, as shown below.

Listing 1. testadd.php

Require_once ' add.php ';
Require_once ' phpunit2/framework/testcase.php ';

Class Testadd extends Phpunit2_framework_testcase
function Test1 () {$this->asserttrue (Add (1, 2) = = 3);}
function Test2 () {$this->asserttrue (Add (1, 1) = = 2);}

This Testadd class has two methods, all using the test prefix. Each method defines a test that can be as simple or complex as listing 1. In this case, we simply concluded in the first test that 1 plus 2 equals 3, and in the second Test, 1 plus 1 equals 2.

The PHPUNIT2 system defines the Asserttrue () method, which is used to test whether the condition values contained in the parameter are true. We then wrote the add.php module, which initially gave it the wrong result.

Listing 2. add.php

function add ($a, $b) {return 0;}

Both tests will fail when you run the unit test now.

Listing 3: Test failure

% PHPUnit testadd.php
PHPUnit 2.2.1 by Sebastian Bergmann.


There were 2 failures:
1) test1 (Testadd)
2) test2 (Testadd)

Tests Run:2, Failures:2, errors:0, incomplete tests:0.

Now I know both of these tests are working properly. Therefore, you can modify the Add () function to really do the actual thing.

function add ($a, $b) {return $a + $b;}

Both of these tests are now available.

Listing 4. Test Pass

% PHPUnit testadd.php
PHPUnit 2.2.1 by Sebastian Bergmann.
OK (2 Tests)

Although the example of this test-driven development is very simple, we can appreciate its thought. We first created the test case, and there was enough code to run the test, but the result was wrong. Then we verify that the test was a failure, and then implemented the actual code to make the test pass.

I found that I was constantly adding code as I implemented the code until I had a complete test that covered all the code paths. At the end of this article, you'll see some suggestions for what tests to write and how to write them.

1 true techarticle The Web application runs 24x7, so my program is still running this problem will haunt me at night. Unit testing has helped me to build enough letters for my Code ...

  • Related Article

    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: 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.