Configuring PHPUnit
Configuring PHPUnit
So far, we’ve been running PHPUnit using this command:
./vendor/bin/phpunit tests
It works, but as your project grows, it’s helpful to have a configuration file so you don’t have to repeat the same options every time.
Let’s walk through creating and simplifying a phpunit.xml
file.
Step 1: Generate the Config File
PHPUnit can generate a starter config file for you:
./vendor/bin/phpunit --generate-configuration
This will create a phpunit.xml
file in your project root.
Step 2: Review the Generated File
The file contains a lot of options, including strict settings for deprecations, risky tests, and code coverage.
For example, it includes:
requireCoverageMetadata="true"
failOnRisky="true"
failOnWarning="true"
You might also see warnings about missing coverage annotations if your tests don’t specify them. But we’re not covering code coverage just yet — so let’s trim this down.
Here’s a minimal version to use:
This does the basics:
- Sets up autoloading with
bootstrap="vendor/autoload.php"
- Defines the
tests/
directory as the test suite - Tells PHPUnit to include source files in
src/
- Enables colored output in the terminal for easier readability
To enable colors, you can add this line inside the root <phpunit>
tag:
colors="true"
Your test output will now be easier to read — with green for passing tests, red for failing ones, and yellow for warnings or skipped tests.
Save your changes to phpunit.xml
, and now you can run tests with:
./vendor/bin/phpunit
No need to specify the tests/
directory — it’s now the default.
0 comments