The Web application runs 24x7 without interruption, so whether or not my program is still running will keep bothering me at night. Unit testing has helped me build enough confidence in my code-so that I can have a good night's sleep.
Unit testing is a framework for writing test cases for code and automatically running these tests. Test-driven development is a unit test method. The idea is that you should first compile the test program and verify that these tests can discover errors before writing the code that needs to pass these tests. When all tests are passed, the features we developed will be completed. The value of these unit tests is that we can run them at any time-either after major modifications, or after being deployed to a running system before code is checked in.
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 installing this framework, You can compile unit tests by creating a test class derived from PHPUnit2_Framework_TestCase.
Module unit test
I found that the best place to start a unit test is in the business logic module of the application. I used a simple example: this is a function that sums two numbers. To start the test, we first compile 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, both of which use the test prefix. Each method defines a test, which can be as simple and complex as listing 1. In this example, in the first test, we simply concluded that 1 and 2 are equal to 3. In the second test, 1 and 1 are equal to 2.
The PHPUnit2 system defines the assertTrue () method, which is used to test whether the condition value in the parameter is true. Then, we wrote the Add. php module, which initially generated an error.
Listing 2. Add. php
function add( $a, $b ) { return 0; } ?>
|
When you run the unit test, both tests will fail.
Listing 3. Test failed
% phpunit TestAdd.php PHPUnit 2.2.1 by Sebastian Bergmann. FF Time: 0.0031270980834961 There were 2 failures: 1) test1(TestAdd) 2) test2(TestAdd) FAILURES!!! Tests run: 2, Failures: 2, Errors: 0, Incomplete Tests: 0.
|
Now I know that both tests can work normally. Therefore, you can modify the add () function to actually do things.
function add( $a, $b ) { return $a+$b; } ?>
|
Now both tests can pass.
Listing 4. Test passed
% phpunit TestAdd.php PHPUnit 2.2.1 by Sebastian Bergmann. .. Time: 0.0023679733276367 OK (2 tests) %
|
Although this test-driven development example is very simple, we can understand its ideas. We first created a test case and had enough code to run the test, but the result was incorrect. Then we verify that the test is indeed a failure, and then implement the actual code to make the test pass.
I found that when implementing code, I always add code until I have a complete test that covers all code paths. At the end of this article, you will see some suggestions on what tests to write and how to write them.
1