Over a million developers have joined DZone.
Platinum Partner

Practical PHP Testing Patterns: Test Suite

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

A Test Suite object implements the same interface of a Testcase Class, but it is in fact a collection of Testcase Objects instead of a single one. When run, this Test Suite will aggregate the execution of all the contained tests.

As you may have correctly guessed, this is the Composite pattern applied to Testcase Classes.

It is handy to be able to run a single test case, but also a set of carefully chosen test cases. For example, they may demonstrate a particular bug. Or they may be slow acceptance tests which should be run only before checking in the code.

This need is filled by the Test Suite pattern, the decouples the Test Runner from the actual number of tests run.

The old way

In previous versions of PHPUnit, you could build a Test Suite Object by hand, by subclassing a Test Suite base class and specifying the classes of the single test cases (which had to be included). These are the famous AllTests.php files which sometimes you sometimes still see in open source projects.

Nowadays automated Test Discovery is put at work, so that you can specify a folder, with optional filters, and have a Test Suite built for you by PHPUnit itself. Configuration has then overcome imperative definition.

Here's how a manually defined Test Suite it works (or, worked):

require_once 'DummyTest.php';

class AllTests
public static function suite()
$suite = new PHPUnit_Framework_TestSuite('PHPUnit Framework');


return $suite;

This pattern is called Test Enumeration, and won't be covered in a different article since it is very simple, and not really used anymore.

The problem with this approach is that the AllTests.php files quickly get out of hand, and become hard to maintain. It's easy to slip and add a test as a .php script but not in the AllTests.php list. Fortunately, at least the larger AllTests.php files could compose the smaller suites recursively, so that you had to add your file only to a single list.

Thus, manually defined Test Suites can be handy for edge cases, but are usually out of place in PHPUnit due to its declaration-based expressive power.

Declarative power

As we'll see in Test Discovery shortly, you can easily run:

phpunit folder/

to execute all the tests in folder/; directories were the primary mean of aggregation that AllTests.php files were used for. While I'm writing this article, <testsuites> tag is functionally equivalent to <testsuite>, but Sebastian may have some surprises for us in the future.

An effective way of building Test Suites is the @group annotation, which works exactly like a tag (folksonomy if you prefer):

You can define any number of @group annotations on a Testcase Class:

* @group functional
* @group ticket-23
class MyControllerTest extends PHPUnit_Framework_TestCase...

and you can pass any number of groups to select only the tests belonging to one of those groups:

phpunit --group functional
phpunit --group functional,acceptance
In the next article in this series, Test Discovery, we will see how to define finer-grained test suites, and to exclude or include tests at will in our definition without resorting to AllTests.php files.

The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}