Recipe 20.13. Writing a Unit Test


20.13.1. Problem

You're working on a project that extends a set of core functionality, and you want an easy way to make sure everything still works as the project grows.

20.13.2. Solution

Write a unit test that tests the core functionality of a function or class and alerts you if something breaks.

A sample test using PHP-QA's .phpt testing system is:

--TEST-- str_replace() function --FILE-- <?php $str = 'Hello, all!'; var_dump(str_replace('all', 'world', $str)); ?> --EXPECT-- string(13) "Hello, world!"

20.13.3. Discussion

There are a number of ways to write unit tests in PHP. A series of simple .phpt tests may be adequate for your needs, or you may benefit from more structured testing solutions such as PHPUnit or SimpleTest. We'll discuss each approach, but the first question is: why write a unit test in the first place?

Writing an application from scratch in any language is a lot like peeling an onion, only in reverse. You start with the center of the onion, and build layers on top of layers until you get to the finished product: an onion.

The more layers you build on top of your core, the more important it is for that core to continue functioning as you expect it to. The easiset way to ensure that the core of an application continues functioning as expected, especially after modifications, is through unit tests.

In the earlier example, we're testing that the str_replace( ) function successfully replaces one string with another. The test doesn't care how the str_replace( ) function is written; all that matters is that it works as expected on a recurring basis.

The easiest way to run the .phpt test is to save it in a file ending in .phpt (str_replace.phpt, for example), and then use PEAR's built-in .phpt execution tool, like this:

% pear run-tests str_replace.phpt

You'll see output like this:

Running 1 tests PASS str_replace() function[str_replace.phpt] TOTAL TIME: 00:00 1 PASSED TESTS 0 SKIPPED TESTS

You can test a number of features of your core functionality by creating multiple .phpt files, and executing:

% pear run-tests *.phpt

For full details on the structure of .phpt files, visit http://qa.php.net/write-test.php.

You can also write unit tests using the PHPUnit/PHPUnit2 unit testing framework. PHPUnit2 is for PHP 5 only, whereas PHPUnit will work under PHP 4 or PHP 5.

The example above could be written as a PHPUnit2 test like this:

require_once 'PHPUnit2/Framework/TestCase.php'; class StrreplaceTest extends PHPUnit2_Framework_TestCase {     public function testStrreplaceWorks()     {         $str = 'Hello, all!';         $this->assertEquals('Hello, world!', str_replace('all', 'world', $str));     } }

Save this code in a file named StrreplaceTest.php. Once PHPUnit2 is installed, you can run the test like this:

% phpunit StrreplaceTest

That command will look for the file named StrreplaceTest.php and run the test defined within it.

PHPUnit is a very powerful unit testing framework that can do much more than run a simple test like in the example. For complete documentation, visit http://www.phpunit.de.

Another popular unit testing framework is called SimpleTest. By default, its tests are intended to be run in a web browser (though a command-line option exists as well). Our example test of the str_replace( ) function would be written like this using SimpleTest:

require_once 'simpletest/unit_tester.php'; require_once 'simpletest/reporter.php'; class TestStrreplace extends UnitTestCase {     function testStrreplace()     {         $str = 'Hello, all!';         $this->assertEqual('Hello, world!', str_replace('all', 'world', $str));     } }

As you can see in this simple example, SimpleTest is quite similar to PHPUnit. However, the SimpleTest framework differs from PHPUnit in its full feature set, and it's best to conduct a thorough comparison of the two frameworks before deciding on which one is best for you. Learn more about SimpleTest at http://www.lastcraft.com/simple_test.php.

20.13.4. See Also

Documentation on .phpt unit tests at http://qa.php.net/write-test.php; on PHPUnit and PHPUnit2 at http://www.phpunit.de; on SimpleTest at http://www.lastcraft.com/simple_test.php.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

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