Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate PHPUnit code coverage while ignoring @covers

I am generating code coverage reports with PHPUnit

vendor/bin/phpunit --coverage-clover coverage.clover

I have a few codebase that use the @covers tag in their tests, indicating the service they test. In these codebases there are no dedicated tests for things such as value objects. This results in the coverage reports indicating a lot of code as not tested, while in fact it is. This makes it hard to find actual untested code by looking at the coverage reports. One way to fix this is to remove the @covers tags, though these are useful to indicate intent and they aid navigation as the IDE recognizes them. Hence I would like PHPUnit to ignore the tags.

I found that there is a --disable-coverage-ignore flag, which makes PHPUnit ignore the coverage ignore tags. What I am looking for is essentially the opposite.

Is there a way to generate code coverage reports that count all executed lines without making changes through these entire codebases such as removing all @covers tags?

like image 262
Jeroen De Dauw Avatar asked Oct 29 '22 19:10

Jeroen De Dauw


2 Answers

There is a PR created to implement this, but unfortunetely never merged. As a quick & dirty working hack, you can add a line into PHPUnit's source code. Just add return false; as the first line of applyCoversAnnotationFilter method in vendor/phpunit/php-code-coverage/src/CodeCoverage.php file, like this:

 private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed, $ignoreForceCoversAnnotation)
{
    return false;

Works for me in PHPUnit 6.5.14.

like image 141
Vasiliy Avatar answered Nov 12 '22 11:11

Vasiliy


According to PhpUnit's manual: "If provided, this effectively filters the code coverage report to include executed code from the referenced code parts only. This will make sure that code is only marked as covered if there are dedicated tests for it, but not if it used indirectly by the tests for a different class, thus avoiding false positives for code coverage."

So by adding @coverage you ask the system to mark only the content of the referenced parts. If you would like to check the coverage of other parts, you may want to remove the annotation, or create other tests to cover those ones, too, or create an other test without the @covers.

If you set "forceCoversAnnotation" to "false" in phpunit.xml, you can create teszt with and without @covers annotation according to your needs.

(My current version is 8.3.4)

like image 20
Tyll Avatar answered Nov 12 '22 13:11

Tyll