phpunit
phpunit copied to clipboard
Improve error when configured code coverage file list is empty
Q | A |
---|---|
PHPUnit version | 9.3.8 |
PHP version | 7.4.8 |
Installation Method | Composer |
Summary
When running phpunit in a more or less empty project, "Incorrect filter configuration" is shown when there are no files to cover. This is misleadig.
Current behavior
A misleading error message Warning: Incorrect filter configuration, code coverage will not be processed
is shown when no source files present in covered path(s)
How to reproduce
- Clone https://github.com/tlamy/phpunit-misleading-error
- run
composer install
- run
composer test-coverage
Expected behavior
Show no error message (maybe a warning that there are no files to cover), but 0% coverage, and "No tests executed".
As soon as a (even an empty) .php file is added to the src directory, the expected behaviour is shown.
This is a known issue (at least to me). I agree that it is confusing, especially to new users. While I do not have time to work on this myself, I would gladly review/merge a pull request.
I'll have a look into this, maybe this weekend
I just hit this myself on a non-empty project. Turns out my coverage section was wrong I had
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
instead of
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src</directory>
</include>
</coverage>
(the directory is relative to the location of the config, so it needed to go up one and then into the src folder).
So I think a change to this reporting no files found as a separate message would be helpful to report the directories it actually searched (would have cottoned me onto my mistake sooner).
It would be nice if we could also just skip this check. I have a project template repository that has no files inside but runs all the tools to see if my CI configs work.
Hello just landed here as a project I worked on suddenly stopped to work properly when asking to run tests with coverage.
What was very strange to my mind was that no update of PHPUnit had been integrated lately in the project. Still, it did require the following new lines in phpunit.xml to work again:
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">app</directory>
</include>
</coverage>
The application being based on Laravel, the app
directory is the conventional one, at least for this project where phpunit.xml is stored in the same directory as app
.
So, going back to the issue topic, I would have find far more relevant to have something like:
Warning: Incorrect filter configuration, code coverage will not be processed
Details: No `<coverage>` tag and proper children where filled in phpunit.xml
Possible fix example:
<coverage processUncoveredFiles="true">
<include>
<!-- The directory target is interpreted relatively to the phpunit.xml file -->
<directory suffix=".php">../source/code</directory>
</include>
</coverage>
actually, msgpix suggestion might not work. I had this exact error. Even though phpunit was running all the tests, all the files which were supposed to be in src where in root directory. You may not need that entire coverage section at all. I don't have it anyway.
Same for me getting below warning
PHPUnit 10.5.9 by Sebastian Bergmann and contributors.
Runtime: PHP 8.3.2 with Xdebug 3.3.1
Configuration: /var/www/html/phpunit.xml
Random Seed: 1706493643
. 1 / 1 (100%)
Time: 00:04.241, Memory: 16.00 MB
There was 1 PHPUnit test runner warning:
1) Test results may not be as expected because the XML configuration file did not pass validation:
Line 24:
- Element 'include': This element is not expected.
WARNINGS!
Tests: 1, Assertions: 1, Warnings: 1.
to remove warning I'm using --coverage-filter cli parameter for now
@nabby27 created a $20.00 reward using Opire
How to earn this reward?
Since this project does not have Opire bot installed yet 😞 you need to go to Opire and claim the rewards through your programmer's dashboard once you have your PR ready 💪
@sebastianbergmann how is this issue going ? I'd like to work on this one.