Home arrow PHP arrow Page 2 - Unit Testing

An Introduction to Unit Testing - PHP

Having a formalized unit testing infrastructure for your projects will save you time in the long run, especially when looking for bugs in code. This article introduces you to setting up a unit testing framework. It is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).

  1. Unit Testing
  2. An Introduction to Unit Testing
  3. Adding Multiple Tests
  4. Inline Packaging
By: Sams Publishing
Rating: starstarstarstarstar / 6
October 19, 2006

print this article



To be successful, a unit testing framework needs to have certain properties, including the following:

  • Automated—The system should run all the tests necessary with no interaction from the programmer.

  • Easy to write—The system must be easy to use.

  • Extensible—To streamline efforts and minimize duplication of work, you should be able to reuse existing tests when creating new ones.

To actually benefit from unit testing, we need to make sure our tests have certain properties:

  • Comprehensive—Tests should completely test all function/class APIs. You should ensure not only that the function APIs work as expected, but also that they fail correctly when improper data is passed to them. Furthermore, you should write tests for any bugs discovered over the life of the library. Partial tests leave holes that can lead to errors when refactoring or to old bugs reappearing.

  • Reusable—Tests should be general enough to usefully test their targets again and again. The tests will be permanent fixtures that are maintained and used to verify the library over its entire life span.

Writing Unit Tests for Automated Unit Testing

For the testing framework discussed in this chapter, we will use PEAR's PHPUnit. PHPUnit, like most of the free unit testing frameworks, is based closely on JUnit, Erich Gamma and Kent Beck's excellent unit testing suite for Java.

Installing PHPUnit is just a matter of running the following (which most likely needs root access):

# pear install phpunit

Alternatively, you can download PHPUnit from http://pear.php.net/PHPUnit.

Writing Your First Unit Test

A unit test consists of a collection of test cases. A test case is designed to check the outcome of a particular scenario. The scenario can be something as simple as testing the result of a single function or testing the result of a set of complex operations.

A test case in PHPUnit is a subclass of the PHPUnit_Framework_TestCase class. An instance of PHPUnit_Framework_TestCase is one or several test cases, together with optional setup and tear-down code.

The simplest test case implements a single test. Let's write a test to validate the behavior of a simple email address parser. The parser will break an RFC 822 email address into its component parts.

class EmailAddress {
public $localPart;
public $domain;
public $address;
public function _ _construct($address = null) {
if($address) {
$this->address = $address;
protected function extract() {
list($this->localPart, $this->domain) =
explode("@", $this->address); } }

To create a test for this, you create a TestCase class that contains a method that tests that a known email address is correctly broken into its components:

require_once "EmailAddress.inc";
require_once 'PHPUnit/Framework/TestClass.php';
class EmailAddressTest extends
PHPUnit_Framework_TestCase { public function _ _constructor($name) { parent::_ _constructor($name); } function testLocalPart() { $email = new EmailAddress("george@omniti.com"); // check that the local part of the address is
equal to 'george' $this->assertTrue($email->localPart == 'george'); } }

Then you need to register the test class. You instantiate a PHPUnit_Framework_ TestSuite object and the test case to it:

require_omce "PHPUnit/Framework/TestSuite";
$suite = new PHPUnit_Framework_TestSuite();

After you have done this, you run the test:

require_once "PHPUnit/TextUI/TestRunner";

You get the following results, which you can print:

PHPUnit 1.0.0-dev by Sebastian Bergmann.
Time: 0.00156390666962
OK (1 test)

>>> More PHP Articles          >>> More By Sams Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: