DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Practical PHP Testing Patterns: Test Suite

Practical PHP Testing Patterns: Test Suite

Giorgio Sironi user avatar by
Giorgio Sironi
·
Dec. 20, 10 · Interview
Like (0)
Save
Tweet
Share
1.36K 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

  • How To Create a Failover Client Using the Hazelcast Viridian Serverless
  • Keep Your Application Secrets Secret
  • Multi-Tenant Architecture for a SaaS Application on AWS
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: