Prev Next

Chapter 7. Organizing Test Suites

One of the goals of PHPUnit (see Chapter 2) is that tests should be composable: we want to be able to run any number or combination of tests together, for instance all tests for the whole project, or the tests for all classes of a component that is part of the project, or just the tests for a single class.

The PHPUnit_Framework_TestSuite class of the PHPUnit framework allows us to organize tests into a hierarchy of test suites. Let us look at PHPUnit's own test suite as an example.

Example 7.1 shows a cut-down version of Tests/AllTests.php, Example 7.2 a cut-down version of Tests/Framework/AllTests.php.

Example 7.1: The AllTests class

<?php
require_once 'PHPUnit/Framework.php';

require_once 'Framework/AllTests.php';
// ...

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

$suite->addTest(Framework_AllTests::suite());
// ...

return $suite;
}
}
?>

Example 7.2: The Framework_AllTests class

<?php
require_once 'PHPUnit/Framework.php';

require_once 'Framework/AssertTest.php';
// ...

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

$suite->addTestSuite('Framework_AssertTest');
// ...

return $suite;
}
}
?>

The Framework_AssertTest class is a normal test case class that extends the PHPUnit_Framework_TestCase base class.

  • Executing phpunit AllTests in the Tests directory will run all tests.

  • Executing phpunit Framework_AllTests AllTests.php in the Tests/Framework directory will run only the tests for the PHPUnit_Framework_* classes.

  • Executing phpunit Framework_AssertTest AssertTest.php in the Tests/Framework directory will run only the tests for the PHPUnit_Framework_Assert class.

  • Executing phpunit --filter testFail Framework_AssertTest AssertTest.php in the Tests/Framework directory will run only the test named testFail from the Framework_AssertTest class.

Suite-Level Setup

The PHPUnit_Framework_TestSuite class offers two template methods, setUp() and tearDown(), that are called before the first test of the test suite and after the last test of the test suite, respectively.

Example 7.3: The MySuite class

<?php
require_once 'MyTest.php';

class MySuite extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
return new MySuite('MyTest');
}

protected function setUp()
{
print "\nMySuite::setUp()";
}

protected function tearDown()
{
print "\nMySuite::tearDown()";
}
}
?>

The MyTest test case class that is added to the test suite MySuite in Example 7.3 has two test methods, testOne() and testTwo() as well as the setUp() and tearDown() methods. The output shows in which order these eight methods are called:

MySuite::setUp()
MyTest::setUp()
MyTest::testOne()
MyTest::tearDown()
MyTest::setUp()
MyTest::testTwo()
MyTest::tearDown()
MySuite::tearDown()

Variables stored in $this->sharedFixture by the setUp() method of the PHPUnit_Framework_TestSuite class are available as $this->sharedFixture in all the test that are aggregated by the test suite object (see the section called “Sharing Fixture”).

Please note that a TestSuite's setUp() and tearDown() methods will be called even if no test of the test suite is run because it is, for instance, filtered.

Prev Next
1. Automating Tests
2. PHPUnit's Goals
3. Installing PHPUnit
4. Writing Tests for PHPUnit
Data Providers
Testing Exceptions
Testing PHP Errors
5. The Command-Line Test Runner
6. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
7. Organizing Test Suites
Suite-Level Setup
8. TestCase Extensions
Testing Output
Testing Performance
9. Database Testing
Datasets
Flat XML Data Set
XML Data Set
Operations
Database Testing Best Practices
10. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
11. Mock Objects
Self-Shunting
Stubs
12. Testing Practices
During Development
During Debugging
13. Test-First Programming
BankAccount Example
14. Code Coverage Analysis
Specifying Covered Methods
Ignoring Code Blocks
Including and Excluding Files
15. Other Uses for Tests
Agile Documentation
Cross-Team Tests
16. Logging
XML Format
Code Coverage (XML)
JavaScript Object Notation (JSON)
Test Anything Protocol (TAP)
GraphViz Markup
Test Database
17. Skeleton Generator
Annotations
18. PHPUnit and Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
19. Continuous Integration
CruiseControl
phpUnderControl
Apache Maven
20. PHPUnit's Implementation
21. PHPUnit API
Overview
PHPUnit_Framework_Assert
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
Package Structure
22. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Assert Classes
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
Subclass PHPUnit_Framework_TestResult
Implement PHPUnit_Framework_TestListener
New Test Runner
A. Assertions
B. The XML Configuration File
Test Suite
Groups
Including and Excluding Files for Code Coverage
Logging
PMD Rules
Setting PHP INI settings and Global Variables
C. PHPUnit for PHP 4
D. Index
E. Bibliography
F. Copyright