DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Practical PHP Testing Patterns: Test Suite

Practical PHP Testing Patterns: Test Suite

Giorgio Sironi user avatar by
Giorgio Sironi
·
Dec. 20, 10 · Web Dev Zone · Interview
Like (0)
Save
Tweet
1.08K Views

Join the DZone community and get the full member experience.

Join For Free

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

<?php
require_once 'DummyTest.php';

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

$suite->addTestSuite('DummyTest');

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.
Testing Test suite PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Learn the Weekly Rituals You Should Master as a Software Project Manager
  • 9 Strategies to Improve Your Software Development Process
  • SQL vs. NoSQL: Pros and Cons

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo