5. Organizing Tests

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.

Composing a Test Suite Using the Filesystem

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
sebastianbergmann/money
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
src directory:

A drawback of this approach is that we have no control over the order in
which the tests are run. This can lead to problems with regard to test
dependencies, see Test Dependencies.
In the next section you will see how you can make the test execution
order explicit by using the XML configuration file.

Composing a Test Suite Using XML Configuration

PHPUnit’s XML configuration file (The XML Configuration File)
can also be used to compose a test suite.
Example 5.1
shows a minimal phpunit.xml file that will add all
*Test classes that are found in
*Test.php files when the tests
directory is recursively traversed.