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:

  1. src tests
  2. `-- Currency.php `-- CurrencyTest.php
  3. `-- IntlFormatter.php `-- IntlFormatterTest.php
  4. `-- Money.php `-- MoneyTest.php
  5. `-- autoload.php

To run all tests for the library we need to point the PHPUnit command-line test runner to the test directory:

  1. $ phpunit --bootstrap src/autoload.php tests
  2. PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
  3. .................................
  4. Time: 636 ms, Memory: 3.50Mb
  5. OK (33 tests, 52 assertions)

Note

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 CurrencyTest test case class in tests/CurrencyTest.php we can use the following command:

  1. $ phpunit --bootstrap src/autoload.php tests/CurrencyTest.php
  2. PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
  3. ........
  4. Time: 280 ms, Memory: 2.75Mb
  5. OK (8 tests, 8 assertions)

For more fine-grained control of which tests to run we can use the --filter option:

  1. $ phpunit --bootstrap src/autoload.php --filter testObjectCanBeConstructedForValidConstructorArgument tests
  2. PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
  3. ..
  4. Time: 167 ms, Memory: 3.00Mb
  5. OK (2 test, 2 assertions)

Note

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.

Example 5.1 Composing a Test Suite Using XML Configuration

  1. <phpunit bootstrap="src/autoload.php">
  2. <testsuites>
  3. <testsuite name="money">
  4. <directory>tests</directory>
  5. </testsuite>
  6. </testsuites>
  7. </phpunit>

To run the test suite, use the the --testsuite option:

  1. $ phpunit --bootstrap src/autoload.php --testsuite money
  2. PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
  3. ..
  4. Time: 167 ms, Memory: 3.00Mb
  5. OK (2 test, 2 assertions)

If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.

The order in which tests are executed can be made explicit:

Example 5.2 Composing a Test Suite Using XML Configuration

  1. <phpunit bootstrap="src/autoload.php">
  2. <testsuites>
  3. <testsuite name="money">
  4. <file>tests/IntlFormatterTest.php</file>
  5. <file>tests/MoneyTest.php</file>
  6. <file>tests/CurrencyTest.php</file>
  7. </testsuite>
  8. </testsuites>
  9. </phpunit>