-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Code coverage not recorded for setting of static variables #913
Comments
CC @derickr |
There can be many reasons why a specific line is not covered. Each of them is likely a different issue. In order to be able to reproduce anything and not get bogged down with details, you need to provide the following in ONE new issue per case. That means:
This information should be filed as one specific case per issue. |
Working on setting up a reproduction example now (as you surmised the code above was just a code pattern, not actual code with which the issue could be reproduced). |
Please also add which Xdebug version you're using, as sometimes I do fix these problems. For example https://bugs.xdebug.org/view.php?id=2075 that was fixed in 3.1.4. |
Hmm.. this is weird... both of the above classes use the exact same code pattern as outlined above, but when using a minimal code sample, I can't reproduce it. Will keep trying to figure out a minimal reproduction scenario, but may be (a lot) harder than I thought. As for the
So, yes, looks like the Xdebug version is out of date, so maybe I should chase that a little and only continue work on the reproduction sample once I have confirmed that the issue still exists with Xdebug 3.1.4. |
FWIW, I don't think this is an Xdebug issue. It's very likely something with this code being run once before the tests are run. Because the static persists between multiple calls to the function, it's very possible that this just "cashed". What happens if you use process isolation? (https://phpunit.readthedocs.io/en/9.5/configuration.html#the-processisolation-attribute) |
I managed to get a test run done with Xdebug 3.1.4, which didn't change anything.
@derickr That's a very good point. As depending on the order in which the tests are run, this could be the case for these functions. I've just done a review of the use of function local static variables across the whole codebase now. Will let you know the result. 🤞🏻 |
Yeah! @derickr was right! Thank you so much for pointing this out to me. You know how you can stare yourself blind on something ? Yes, that. 🤗 I will close this issue, but before I do, let me recap the way I ended up fixing it in case other people run into the same. Using the annotation(s) turned out to be a no-go.In most cases/for most people this will probably work fine, but for this particular codebase it did not. I started by adding The particular build which failed was on PHP 5.4 with PHPUnit 4.8.36 in combination with PHP_CodeSniffer 2.6.0 (runtime dependency for the codebase with PHPCS 2.6.0 being the lowest supported version). While I would truly and dearly hope nobody would still use such combinations, I unfortunately know from the PHPCS stats that's not yet the case, which means that this codebase still has to support it and that includes safeguarding that support via the tests. Using separate test suites worksNot sure where I got this from, but I remembered that I'd run into issues with In my original <testsuites>
<testsuite name="PHPCSUtils">
<directory suffix="Test.php">./Tests/</directory>
</testsuite>
</testsuites> I've now changed it to the below, which in effect means the three test classes which test code which declare function local <testsuites>
<testsuite name="Isolation-EmptyTokens">
<file>./Tests/BackCompat/BCTokens/EmptyTokensTest.php</file>
</testsuite>
<testsuite name="Isolation-NamespaceType">
<file>./Tests/Utils/Namespaces/NamespaceTypeTest.php</file>
</testsuite>
<testsuite name="Isolation-GetCompleteNumber">
<file>./Tests/Utils/Numbers/GetCompleteNumberTest.php</file>
</testsuite>
<testsuite name="PHPCSUtils">
<directory suffix="Test.php">./Tests/</directory>
<exclude>Tests/BackCompat/BCTokens/EmptyTokensTest.php</exclude>
<exclude>Tests/Utils/Namespaces/NamespaceTypeTest.php</exclude>
<exclude>Tests/Utils/Numbers/GetCompleteNumberTest.php</exclude>
</testsuite>
</testsuites> I couldn't find the I'll tidy up my WIP branch tomorrow and merge the changes. Very happy to have been able to figure this one out with your help. Thank you! |
The functions being tested in these three test classes all use function local `static` variables for optimization. Having the function local `static` variable will often prevent code coverage from being recorded correctly for the code as the code for setting the static will only be run when the function is first called, which may not be in the dedicated test for the function, so it makes code coverage recording highly dependant on the order in which tests are run. In these three cases, I do believe the function local `static` variable is justified (for the time being, benchmarking should be able to determine for sure at some point in the future). To still prevent missed lines in the code coverage reports, I'm moving the tests for these three functions into a separate test suite, which will run first. This should ensure the code which sets the static will be run first when the dedicated tests for these methods are run and should allow the correct registration of code coverage for this code. Mind: if at some point the `executionOrder` configuration would be added to the config/enabled, it needs to be verified how that behaves in combination with multiple test suites. But that's for later. Ref: sebastianbergmann/php-code-coverage#913
The functions being tested in these three test classes all use function local `static` variables for optimization. Having the function local `static` variable will often prevent code coverage from being recorded correctly for the code as the code for setting the static will only be run when the function is first called, which may not be in the dedicated test for the function, so it makes code coverage recording highly dependant on the order in which tests are run. In these three cases, I do believe the function local `static` variable is justified (for the time being, benchmarking should be able to determine for sure at some point in the future). To still prevent missed lines in the code coverage reports, I'm moving the tests for these three functions into a separate test suite, which will run first. This should ensure the code which sets the static will be run first when the dedicated tests for these methods are run and should allow the correct registration of code coverage for this code. Mind: if at some point the `executionOrder` configuration would be added to the config/enabled, it needs to be verified how that behaves in combination with multiple test suites. But that's for later. Ref: sebastianbergmann/php-code-coverage#913
The functions being tested in these three test classes all use function local `static` variables for optimization. Having the function local `static` variable will often prevent code coverage from being recorded correctly for the code as the code for setting the static will only be run when the function is first called, which may not be in the dedicated test for the function, so it makes code coverage recording highly dependant on the order in which tests are run. In these three cases, I do believe the function local `static` variable is justified (for the time being, benchmarking should be able to determine for sure at some point in the future). To still prevent missed lines in the code coverage reports, I'm moving the tests for these three functions into a separate test suite, which will run first. This should ensure the code which sets the static will be run first when the dedicated tests for these methods are run and should allow the correct registration of code coverage for this code. Mind: if at some point the `executionOrder` configuration would be added to the config/enabled, it needs to be verified how that behaves in combination with multiple test suites. But that's for later. Ref: sebastianbergmann/php-code-coverage#913
Given the following code
With a test along the lines of:
I'd expect
That the lines within the condition, which set the static variable would show as covered...
But instead this happened
They don't show as covered.
Additional information
Some examples of coverage reports where this can be seen:
I have a feeling this may be the same issue as #23 which was closed due too little information having been provided to be actionable.
Happy to set up a clonable reproduction case is so desired or alternatively, the PHPCSUtils repo can be used to reproduce the issue.
Please let me know if I can provide any additional details to help debug this.
The text was updated successfully, but these errors were encountered: