PHP Unit Testing Framework 1
A Unit testing framework for PHP.

PHP Unit Testing Framework Documentation

License

A PHP Unit testing framework

Copyright (C) 2011 Ed Heal (ed.heal@yahoo.co.uk)

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Introduction

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

Implement the test cases

To implement a test case you need to extend the class TestCase and implemented 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:

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 will perform logging of the test steps and is used in the creation of the test report.

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

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) thrown an exception the test is considered a failure.

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;
 $suite->AddTest('MyTestCase');
 $suite->AddTest('MyOtherTestCase');

This creates a test suite containg 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:

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).

Finally

If you have any comments, please contact me at ed.heal@yahoo.co.uk.

I hope that this small library will help in the development of good quality PHP software.

 All Data Structures Files Functions Variables

© 2011, Ed Heal