Agile Zone is brought to you in partnership with:

I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 637 posts at DZone. You can read more from them at their website. View Full User Profile

Practical PHP Testing Patterns: Test Automation Framework

11.01.2010
| 9925 views |
  • submit to reddit

A Test Automation Framework is a framework that provides all the boilerplate code used in test suites, so that you can fill in the remaining 20% part of your test suite code with project-specific logic. Automation is the important word in the definition of such a framework: it should be capable of grouping your tests and run them all at the push of a button.

For example, a testing framework provides these utilities:

  • assertions methods, along with statistics on how many of them fail and pass in each run of the test suite.
  • Identification of test methods to run by annotations or names.
  • Isolation of single tests achieved by resetting the state of the test case between runs.
  • Stubbing and mocking facilities, with automatic generation of Test Doubles and management of their behavior.

There are more and more cross-project functionalities that get included into testing frameworks or their extensions, becoming reusable in different codebases.

By the way, the more important functionality of a testing framework resides in relieving you from running Scripted Tests manually, providing a contract your test code can conform to, different from simple .php scripts.

Thus the framework usually accepts a set of adapters conforming to its contract and runs them internally. You do not execute your Scripted Tests by running .php files: you pass them to the executable and it will harness the contained code, including the required library code.

We use PHPUnit, and... and... PHPUnit

This was my response to a Microsoft conference speaker demanding what tools we use in testing PHP applications. PHPUnit is the standard testing framework in the PHP world, and it is now used by Zend Framework and Symfony, the major open source PHP frameworks. Content Management Systems do not really count for testing technologies.

To use PHPUnit, you can write Test Cases as classes that extend PHPUnit_Framework_TestCase, a base superclass provided by the PEAR package.
Each method whose name starts with 'test', like testClosesBookWhenLastPageHasBeenRead(), are discovered with reflection and run automatically, each in a clean scenario consisting in a fresh instance of the test case.

The advantages of a testing framework are clear: it provides you with much boilerplate code but leaves you free of imperatively implement what you want inside a test method. Declarative-only approach are not so diffused, as a programmer has to first re-learn a syntax for writing tests, whereas with xUnit frameworks he can just continue writing in its programming language, albeit inside methods instead of in his own classes.

Here's a comparison of a test written in plain PHP:

<?php
// tests of sqrt()

// square root works with positive numbers
$squareRoot = sqrt(25);
if ($squareRoot != 5) {
    echo "Error: square root of 25 is not 5.\n";
} else {
    echo "Ok.\n";
}

$squareRoot = sqrt(0);
if ($squareRoot != 0) {
    echo "Error: square root of 25 is not 5.\n";
} else {
    echo "Ok.\n";
}

// square root does not work with negative numbers
$squareRoot = sqrt(-1);
if (!is_nan($squareRoot)) {
    echo "Error: square root of 25 is not 5.\n";
} else {
    echo "Ok.\n";
}

 

and one in PHPUnit:

<?php
class SqrtTest extends PHPUnit_Framework_TestCase
{
    public function squareValues()
    {
        return array(
            array(25, 5),
            array(0, 0)
        );
    }

    /**
     * @dataProvider squareValues
     */
    public function testSquareRootWorksWithPositiveNumbers($square, $expectedRoot)
    {
        $root = sqrt($square);
        $this->assertEquals($expectedRoot, $root);
    }

    public function testSquareRootDoesNotWorkWithNegativeNumbers()
    {
        $this->assertTrue(is_nan(sqrt(-1)));
    }
}

If you're experienced with PHPUnit, procedural execution seems primitive, but Symfony 1.x test suite was based on Lime, a small testing framework whose tests are defined as files instead of classes. PHPUnit has become the standard even in Symfony now.

Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)