Recipe 20.14. Writing a Unit Test Suite


20.14.1. Problem

You want to be able to run more than one unit test conveniently on a regular basis.

20.14.2. Solution

Wrap your unit tests into a group known as a unit test suite.

20.14.3. Discussion

It's rare to have a program simple enough that a single unit test will fulfill all the testing needs that it will have during its lifespan. Over time, as applications grow there is a need to add more and more tests, either to test new functionality or verify that fixed bugs stay fixed.

Once your library of tests gets larger than a handful, you'll find it much more convenient to group your tests into a unit test suite. A test suite, despite its formal-sounding name, is just a wrapper around a bunch of tests that can all be run by referring to the name of the test suite.

Using the SimpleTest framework, let's create a test suite to test more than just the str_replace function in PHP. A number of tests related to string functions can be put in a single file. For example, in a file named string_tests.php, let's put:

class TestStringfunctions extends UnitTestCase {     function testStrreplace()     {         $str = 'Hello, all!';         $this->assertEqual('Hello, world!', str_replace('all', 'world', $str));     }     function testSubstr()     {         $str = 'Hello, all!';         $this->assertEqual('e', substr($str, 1, 1));     } }

Now we've got two tests that will be run from the TestStringfunctions class. Let's create a similar file called array_tests.php, with the following tests defined in it:

class TestArrayfunctions extends UnitTestCase {     function testArrayflip()     {         $array = ('foo' => 'bar', 'cheese' => 'hotdog');         $flipped = array_flip($array);         $this->assertEqual('foo', reset($flipped));     }     function testArraypop()     {         $array = ('foo' => 'bar', 'cheese' => 'hotdog');         $popped = array_pop($array);         $this->assertEqual('hotdog', $popped);         $this->assertEqual(1, sizeof($array));     } }

With four tests to run, it's time to put together a suite that will run all of these whenever we want to check to make sure things are working as they should be. Our test suite looks like this:

require_once 'simpletest/unit_tester.php'; require_once 'simpletest/reporter.php'; $test = new GroupTest('All tests'); $test->addTestFile('string_tests.php'); $test->addTestFile('array_tests.php'); if (TextReporter::inCli()) {     exit ($test->run(new TextReporter()) ? 0 : 1); } else {     $test->run(new HtmlReporter()); }

Save this in a file named test_suite.php, and then run it with a browser that has PHP installed properly and paths set properly in the script to reflect the Simple Test installation location.

When run from a shell using the PHP CLI, the result should be similar to:

% php test_suite.php All tests OK Test cases run: 4/4. Failures: 0, Exceptions: 0

Using this approach, you can grow your automated testing system to include a large number of tests and still be able to trigger them all through a single command.

Notice the use of the ternary operator when the tests are run in CLI mode; this method of running the unit test suite allows the script to return a success or failure condition when run as part of an external automated testing script.

20.14.4. See Also

Documentation on SimpleTest at http://www.lastcraft.com/simple_test.php; on PHPUnit and PHPUnit2 at http://www.phpunit.de, which covers test suite creation in PHPUnit.




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

Similar book on Amazon

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