One of the goals of PHPUnit 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.
PHPUnit supports different ways of organizing tests and composing them into a test suite. This chapter shows the most commonly used approaches.
Probably the easiest way to compose a test suite is to keep all test case source files in a test directory. PHPUnit can automatically discover and run the tests by recursively traversing the test directory.
Lets take a look at the test suite of the Object_Freezer
library. Looking at this project's directory structure, we see that the
test case classes in the Tests
directory mirror the
package and class structure of the System Under Test (SUT) in the
Object
directory:
Object Tests |-- Freezer |-- Freezer | |-- HashGenerator | |-- HashGenerator | | `-- NonRecursiveSHA1.php | | `-- NonRecursiveSHA1Test.php | |-- HashGenerator.php | | | |-- IdGenerator | |-- IdGenerator | | `-- UUID.php | | `-- UUIDTest.php | |-- IdGenerator.php | | | |-- LazyProxy.php | | | |-- Storage | |-- Storage | | `-- CouchDB.php | | `-- CouchDB | | | | |-- WithLazyLoadTest.php | | | | `-- WithoutLazyLoadTest.php | |-- Storage.php | |-- StorageTest.php | `-- Util.php | `-- UtilTest.php `-- Freezer.php `-- FreezerTest.php
To run all tests for the library we just need to point the PHPUnit command-line test runner to the test directory:
phpunit Tests
PHPUnit 4.2.0 by Sebastian Bergmann.
............................................................ 60 / 75
...............
Time: 0 seconds
OK (75 tests, 164 assertions)
If you point the PHPUnit command-line test runner to a directory it will
look for *Test.php
files.
To run only the tests that are declared in the Object_FreezerTest
test case class in Tests/FreezerTest.php
we can use
the following command:
phpunit Tests/FreezerTest
PHPUnit 4.2.0 by Sebastian Bergmann.
............................
Time: 0 seconds
OK (28 tests, 60 assertions)
For more fine-grained control of which tests to run we can use the
--filter
option:
phpunit --filter testFreezingAnObjectWorks Tests
PHPUnit 4.2.0 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test, 2 assertions)
A drawback of this approach is that we have no control over the order in which the test are run. This can lead to problems with regard to test dependencies, see the section called “Test Dependencies”. In the next section you will see how you can make the test execution order explicit by using the XML configuration file.
PHPUnit's XML configuration file (Appendix C)
can also be used to compose a test suite.
Example 5.1
shows a minimal example that will add all *Test
classes
that are found in *Test.php
files when the
Tests
is recursively traversed.
Example 5.1: Composing a Test Suite Using XML Configuration
<phpunit> <testsuites> <testsuite name="Object_Freezer"> <directory>Tests</directory> </testsuite> </testsuites> </phpunit>
The order in which tests are executed can be made explicit:
Example 5.2: Composing a Test Suite Using XML Configuration
<phpunit> <testsuites> <testsuite name="Object_Freezer"> <file>Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php</file> <file>Tests/Freezer/IdGenerator/UUIDTest.php</file> <file>Tests/Freezer/UtilTest.php</file> <file>Tests/FreezerTest.php</file> <file>Tests/Freezer/StorageTest.php</file> <file>Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php</file> <file>Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php</file> </testsuite> </testsuites> </phpunit>