Why perform unit testing for PHP?

The main reason for performing PHP unit testing is to discover bugs in the software development life cycle in the earliest stage of development. This will reduce the costs in bug discovery and correction in later stages of software development. These costs are in terms of time, man power and the impact on the project.

Moreover as a software developer that takes pride in the software you develop you can be confident that the deliverables are working as expected and you have proof of this.

How to perform PHP unit testing?

The best way of performing PHP unit testing is to create a series of tests for a class that you have developed and place these tests in an increasing order of complexity. The first set of test cases you develop should concentrate on ensuring that a single method works as expected. Following this you should exercise the class when a combination of methods.

The motivation for testing in this order is that if you discover a single method does not work as expected, it is easier to find, correct and verify the fix is successful without having to run long and complicated tests

The completed set of tests should be constructed so that:

  1. All possible routes through the code are traversed. Sometimes this is not possible dueto error conditions being difficult to simulate;
  2. Exercising the boundary conditions as well as the nominal route.

How to use this PHP unit testing framework!

This is a PHP Unit testing framework that is based upon CPPUnit/JUnit. To implement unit tests using this framework you should do the following steps.

Implement the test cases

To implement a test case you need to extend the class TestCase and implement the functions SetUp, Run and TearDown i.e.

class MyTestCase extends TestCase
  public function SetUp()
    // Code to perform set up for test case.
  public function Run()
    // Code to perform the test case.
    $this->AssertEquals('hello', 'there', 'Should fail!');
    $this->AssertEquals('howdy', 'howdy', 'Should pass!');

  public function TearDown()
    // Code to tidy up afterwards.

The SetUp function should contain all the tasks that are required before the test should begin. For example:

  • Making connections to databases;
  • Constructing objects used for the test;
  • Creating temporary files;
  • Ensuring that the environment is in a suitable state for the test.

The Run method is where the testing occurs. Within this function you use the methods defined in the Assert class to perform assertions. These methods log the test steps and are used in the creation of the test report.

Finally the TearDown method is executed at the end of the test to tidy up. This is always executed and contains tasks such as:

  • Disconnecting from a database;
  • Removing temporary files.

If the SetUp function throws an exception, the Run method will not be called, but the TearDown will be called.

If any of the three methods (SetUp, Run and TearDown) throw an exception the test is considered as failed.

Creating a test suite

Next task is to construct a test suite and add the test cases to it. This is demonstrated by the code snippet below:

$suite = new TestSuite;

This creates a test suite containing two test cases that are defined by the classes 'MyTestCase' and 'MyOtherTestCase'. You can add as many test cases as you wish.

Running the tests

To run the tests you need to construct a test runner. This will define the format of the output of the results.

Currently three have been defined:

  • TextTestRunner - Output as ASCII text;
  • XMLTestRunner - Output as XML;
  • XHTMLTestRunner - Output as XHTML.

The code snippet below shows how to construct a test runner and the run the test suite to generate XHTML output.

$runner = new XHTMLTestRunner;
$runner->Run($suite, '');

Note that the second parameter of the Run method defines if the output goes to stdout, to a named file or to a temporary file (see TestRunner).

And finally...

Included in the download is an example that tests the assertions defined in PHP Unit Test Framework.

I welcome comments and suggestions. Please do not hesitate to contact me at ed.heal@yahoo.co.uk.