-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnitBridge] Added a CoverageListener to enhance the code coverage report #23149
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
Conversation
449e406
to
0afa3cf
Compare
@lyrixx is there a way to opt-out the guessing for some test cases (smoke tests used to cover things through functional tests for instance) ? |
I like it :) |
@stof As soon as you add an annotation on your test class, the listener adds nothing. |
0afa3cf
to
16fe7dc
Compare
Nice! |
This PR did not get so much attraction. IMHO, it's nice. Should I finished it or it will be a waste of time? |
IMO It should be finished, I may use it ;) |
A test listener should only listen. In the future, it will only be able to listen. The proper solution to this problem is to follow best practices and set |
@sebastianbergmann well, this listener is an attempt at guessing the covered class based on a convention instead of forcing to put the annotation everywhere. What would be the proper way to implement such convention-based coverage configuration in PHPUnit ? |
My first reaction was: why is it related to Symfony? Why isn't it something part of PHPUnit itself it that's something useful? |
@fabpot that's a good question. I guess I created the PR in Symfony because I contribute more to Symfony than to PHPUnit. And we already have some listener in Symfony. |
Useful feature 👍 I agree with @lyrixx as others similar listeners are existing in SF. |
@lyrixx Can you add some information in the documentation? |
@fabpot Keep in mind this PR is really not ready. I have to make it work for different PHPUnit version. anyway, what documentation are you referring to? (local PHP doc or the symfony/symfony-docs repo?) |
652c5ef
to
8e4f1aa
Compare
I updated the PR to support PHPUnit 5.* and 6.*. Locally, I also updated all phpunit.xml.dist to add the listener, but the phpunit bridge is not required. Should I update them too? |
IMO, if we already try to figure out what is the expected class to be tested, we could already put it into code and do it only once, commit and forget about it. no need to do it all the times on runtime. anyway, big 👍 for strict coverage |
Would it be possible to add a test, by mean of a phpt for example? this would ensure that we can detect whenever phpunit changes its internals and breaks the feat. |
@nicolas-grekas , why do it in runtime instead of actually store it as annotation at all test files, as I suggest ? |
@keradus the purpose of this PR is exactly to leverage a convention in order to not have to add |
explicit is better than implicit ;) |
how is it possible? and BTW this feature is opt-in ; So if you don't like it, don't use it ;) |
it could as there is no tests, so if one day there will be some newer autoloader spec, dir structure or anything, it could silently fail matching src class for given test class
of course, but still I think it's nice to think about other possible solutions. |
d5f4199
to
110d57c
Compare
Arf, I don't know how to exclude my "Fixture" test from the Symfony test suite. I have try many things without success :/ |
110d57c
to
30381f4
Compare
8b4223d
to
239bc35
Compare
Hi.
|
Still 👍 on my side, ping @symfony/deciders |
👍 |
@lyrixx can you add an entry in the CHANGELOG file please? |
Oups, I forgot to push a commit. Fixed. |
class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListener', 'Symfony\Bridge\PhpUnit\CoverageListener'); | ||
// Using an early return instead of a else does not work when using the PHPUnit | ||
// phar due to some weird PHP behavior (the class gets defined without executing | ||
// the code before it and so the definition is not properly conditional) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fabbot does not seem to be happy with the comments not being indented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a false positive. I ignored it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is that a false positive?
Thank you @lyrixx. |
…e code coverage report (lyrixx) This PR was squashed before being merged into the 3.4 branch (closes #23149). Discussion ---------- [PhpUnitBridge] Added a CoverageListener to enhance the code coverage report | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | symfony/symfony-docs#8416 --- The code coverage computed by PHPUnit is not very accurate by default as it marks a line as tested as soon as it has been executed. For example, if you have two classes A and B where A is using B and you write test only for the class A then the class B will be marked as tested. You can fix this issue by adding `@covers A` on top of the class ATest, but it's a bit boring. This Listener add this annotation on each test if it's applicable: * If an annotation already exists, we do nothing. * We try to find the SUT thanks to the Test class name, if it does not exist, we do nothing --- If you wan to see it in action: https://github.com/lyrixx/phpunit-auto-cover --- The PR is not finished, I think we could add this listener to symfony itself. What do you think? Commits ------- e17206d [PhpUnitBridge] Added a CoverageListener to enhance the code coverage report
…rixx, javiereguiluz) This PR was merged into the 3.4 branch. Discussion ---------- [PHPUnitBridge] Added docs for the CoverageListener For symfony/symfony#23149 Commits ------- c21ab16 Fixed code indentation e78804b Minor rewords 81365f3 [PHPUnitBridge] Added docs for the CoverageListener
The code coverage computed by PHPUnit is not very accurate by default as it marks a line as tested as soon as it has been executed.
For example, if you have two classes A and B where A is using B and you write test only for the class A then the class B will be marked as tested.
You can fix this issue by adding
@covers A
on top of the class ATest, but it's a bit boring.This Listener add this annotation on each test if it's applicable:
If you wan to see it in action: https://github.com/lyrixx/phpunit-auto-cover
The PR is not finished, I think we could add this listener to symfony itself.
What do you think?