Over a million developers have joined DZone.

Practical PHP Testing Patterns: Test Automation Framework

DZone's Guide to

Practical PHP Testing Patterns: Test Automation Framework

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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:

// 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:

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

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}