Hack79.Test Your Code with Unit Tests


Hack 79. Test Your Code with Unit Tests

Use PHPUnit and PHPUnit2 to test your code continuously.

PHP is quietly becoming a force in enterprise application development. With larger applications come more complexity and requirements for testing in an attempt to make sure that the application is stable. It's a truism that stable applications are built by creating a multitiered structure, with each layer well tested on its own. For example, you might have a self-contained database layer (that is tested) that sits underneath a business logic layer (that is tested), which in turn is used by the user interface layer (that is also tested). Starting to get the idea?

To test each layer, it has become common practice to create unit tests. These are tests that are run after any code change and before any code is checked in (you are using version control on your enterprise projects, aren't you?). In fact, it's common to insist that all unit tests must pass before code is even a candidate to be checked back into your code repository. Further, when new bugs are found, new unit tests are created that exercise the faulty code and report the error; then, the error is fixed, and the unit tests should pass, ensuring that if the problem reappears, everyone will know about it (through a failing unit test).

This hack shows how to use the PHPUnit2 framework to develop unit tests for PHP 5, available on the PEAR module distribution network [Hack #2].

For PHP 4, you will want to use the original PHPUnit. This module is also available via PEAR.


8.2.1. The Code

Save the code in Example 8-1 as Add.php.

Example 8-1. A simple add function
 <?php function add( $a, $b ) { return $a + $b; } ?> 

Save the code in Example 8-2 as TestAdd.php. This simple script contains two unit tests, test1( ) and test2( ), both which test the add() method.

Example 8-2. The add function unit tests
 <?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->assertFalse( add( 1, 1 ) == 3 ); } } ?> 

As simple as add( ) seems, it is perfectly natural that you would use (at least) two unit tests for testing. Imagine how many unit tests a typical application would need to be thoroughly tested!

8.2.2. Running the Hack

You run this code using the phpunit test runner:

 % phpunit TestAdd.php PHPUnit 2.2.1 by Sebastian Bergmann. .. Time: 0.0028750896453857 OK (2 tests) % 

This shows the running of the unit test (represented by the two dots), and the results, which in this case are OK since both unit tests passed.

Be sure you give phpunit the name of the unit test script, rather than the actual script being tested.


8.2.3. See Also

  • "Generate Your Unit Tests" [Hack #80]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net