Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lines with 0 executions should be marked as "Non covered" #1060

Open
gufoe opened this issue Dec 17, 2024 · 5 comments
Open

Lines with 0 executions should be marked as "Non covered" #1060

gufoe opened this issue Dec 17, 2024 · 5 comments

Comments

@gufoe
Copy link

gufoe commented Dec 17, 2024

Q A
php-code-coverage version 11.0.8
PHP version 8.3.11
Driver Xdebug
Xdebug version (if used) v3.2.0
Installation Method Composer
Usage Method other

I am new to this library which seems fantastic, but I have found a weird behaviour.
Files that have some lines tested are shown as 100% tested, even if they contain some functions that have never been executed.

I have made a very minimal and self contained example here, you can just run it with php -d xdebug.mode=coverage index.php and you'll find the report in report/index.html.

The report will show the file as 100% coverage (3/3 lines).
But the file does not have 3 lines: it has 41 lines, so i was expecting 3/41 tested (~7% coverage).

Am I getting something wrong or is there a bug in the library?

image
image

Thank you very much for the effort you put into this library, it really helps.

@sebastianbergmann
Copy link
Owner

You need to separate the code that uses this library from the code you want code coverage for.

@gufoe
Copy link
Author

gufoe commented Dec 18, 2024

Ok, I have updated the repo, still the same behaviour.
image

@sebastianbergmann
Copy link
Owner

Please do not use Xdebug's API directly, but only use CodeCoverage::start() and CodeCoverage::stop().

@gufoe
Copy link
Author

gufoe commented Dec 19, 2024

Awesome, that was the issue, thank you! I updated the code in the repo, now the report properly shows 50% of the methods/code is tested.

@gufoe gufoe closed this as completed Dec 19, 2024
@gufoe
Copy link
Author

gufoe commented Feb 10, 2025

Hello its me again!
I found out the issue: it happens when i try to run coverage twice on the same PHP process.
The first time i run the coverage report, it works properly (logs both covered (green) and uncovered (red) lines in the report).
The second time i run the report, it will only log green lines, therefore reporting 100% coverage.

You can find the minimal repository to see the issue here: https://github.com/gufoe/php-debug

This happens in 3 cases:

  • calling the generate report twice from the same php-cli command
  • calling the generate report once per request, on two different requests, using php -S 127.0.0.1:8080 (the first time the report will be correct, the second time it will be broken)
  • same as preivous, but on php fpm. the report will only be correct on the first request, so i would have to restart php-fpm every time i have to generate a report.

Here is the screenshots of the report results, after the first run, and after the second run:

Image

Image

@gufoe gufoe reopened this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants