4. Organizing Tests
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 project.
Looking at this project’s directory structure, we see that the test case classes in the directory mirror the package and class structure of the System Under Test (SUT) in the src
directory:
To run all tests for this project can need to point the PHPUnit command-line test runner to the test directory:
$ ./tools/phpunit --bootstrap tests/bootstrap.php tests
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
Runtime: PHP 8.2.2
............................................................... 63 / 177 ( 35%)
............................................................... 126 / 177 ( 71%)
................................................... 177 / 177 (100%)
Time: 00:17.100, Memory: 28.27 MB
OK (177 tests, 657 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 WorldTest
test case class in tests/unit/WorldTest.php
we can use the following command:
$ ./tools/phpunit --bootstrap src/autoload.php tests/unit --filter test_creating_a_world
Runtime: PHP 8.2.2
. 1 / 1 (100%)
Time: 00:00.077, Memory: 10.00 MB
OK (1 test, 2 assertions)
Composing a Test Suite Using XML Configuration
PHPUnit’s XML configuration file () can also be used to compose a test suite. Example 4.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 4.1 Composing a Test Suite Using XML Configuration
Now that we have an XML configuration file, we can invoke the PHPUnit test runner without arguments (tests
, for instance) or options (--bootstrap
, for instance) to run our tests:
$ ./tools/phpunit
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
Runtime: PHP 8.2.2
Configuration: /path/to/raytracer/phpunit.xml
............................................................... 126 / 177 ( 71%)
................................................... 177 / 177 (100%)
Time: 00:17.100, Memory: 28.27 MB
OK (177 tests, 657 assertions)
The PHPUnit test runner’s --list-suites
option can be used to print a list of all test suites defined in PHPUnit’s XML configuration file:
$ ./tools/phpunit --testsuite unit
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
Runtime: PHP 8.2.2
Configuration: /path/to/raytracer/phpunit.xml
............................................................... 63 / 172 ( 36%)
............................................................... 126 / 172 ( 73%)
.............................................. 172 / 172 (100%)
Time: 00:00.213, Memory: 24.27 MB