Code coverage in Pest tells you which lines of code your test suite executes and which lines it doesn’t. Of course, PHPUnit offers you a beautiful section about this topic: Code Coverage Analysis.
If you're running Xdebug you need to make sure that coverage is enabled in Xdebug. The easiest way to do this is to set the
XDEBUG_MODEenvironment variable to
coveragebefore running your tests. e.g.
XDEBUG_MODE=coverage ./vendor/bin/pest --coverage
Now, in this section, we are going to see the extra options that Pest offers.
--coverage option, gives you a human-readable code coverage
directly on the console.
Keep in mind: the percentage displayed concerns the number of lines of source code that has been tested/executed.
The numbers marked in red (if any) relate to the lines not covered by your current test suite, these are displayed using the line numbers. If there are multiple lines without coverage these numbers will be displayed with
.. between them. For example, if you are missing coverage on line 52, you will see
52 in red, whereas if there is missing coverage between lines 52 and 60 you will see
You can combine this with other coverage options from PHPUnit such as
Of course, you can always combine the
--min option to configure minimum threshold enforcement
for coverage results. If the thresholds are not met, Pest will return failure.
1./vendor/bin/pest --coverage --min=90
By default, the PHPUnit configuration file generated by Pest initialization will include directory coverage for the
Although this covers most libraries and applications, you may need to add more directories to be covered. To do this, you'll need to add a new
<directory> element inside the
If you are experiencing a
Coverage not found in path: vendor/pestphp/.temp/coverage.php exception message, for example, if your code is under a
lib directory, you'll need to apply the following change to your
1<coverage processUncoveredFiles="true">2 <include>3 <directory suffix=".php">./app</directory>4 <directory suffix=".php">./src</directory>5+ <directory suffix=".php">./lib</directory>6 </include>7</coverage>
Next section: CLI Options →