Generate PHPUnit code coverage while ignoring @covers
Asked Answered
L

3

6

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?

Lorenzen answered 29/1, 2018 at 1:15 Comment(2)
Did you find a solution to this?Likely
@Yep_It's_Me unfortunately notLorenzen
S
1

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.

Shaneka answered 30/4, 2019 at 9:49 Comment(0)
N
1

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)

Neurogram answered 23/9, 2019 at 8:46 Comment(1)
What if needs to be dynamic? Sometimes ignore the covers, sometimes add them?Triturable
T
0

You can install tool rawr/covers-ignore from composer, which will remove @covers and @coversNothing annotations from PhpUnit TestCase classes, on which you can later run your test suite

composer require --dev rawr/covers-ignore

and then

php vendor/bin/covers-ignore test/

and then

php vendor/bin/phpunit test/
Triturable answered 9/7, 2022 at 1:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.