Comments (4)
Sure, my "but people are using this" response was mostly a frustrated "this was closed without any discussion WTF" response. I'm still pretty frustrated because people in the original issue are requesting it, I'm requesting it, seems like there's interest so maybe would could have at least discussed before closing it.
Frustrating aside, to your question, as mentioned in the summary its useful for explicitly flagging what the test method is intended to test. This has some benefits
- You can see what code it testing your method when reviewing a method even if the call is indirect.
- You can quickly link back to the code you're testing in your IDE.
- Your coverage test doubles to make sure you're not trying to cover code that no longer exists. This may seem uncommon because your test should be calling the method you're testing. However this has flagged dead tests for me quite a few times and the amount of time I've spent tracking broken tests on projects to code that hasn't existed for multiple versions tells me this is actually valuable.
- Reports can be cleaner. If you have a class that has two methods and a couple hundred provided arguments, if you have any interaction between the methods you get these crazy big reports where you get cross coverage that makes detailed reports kinda useless. A specific XSS class comes to mind which has a test and a filtering method and thousands of test cases. The test is better by being able to split the tests and the report is exponentially smaller by being able to assign the coverage.
I think this generally starts being useful in more complicated systems where there's "magic" happening. Drupal has a lot of this but I really appreciate this when writing tests for Laravel which more people here might be familiar with. Here's an overly simplified test that would test a Controller in Laravel:
<?php
use Illuminate\Foundation\Testing\TestCase;
/**
* @coversDefaultClass \App\Http\Controllers\ContactController
*/
class FooControllerTest extends TestCase {
/**
* @covers ::index
*/
public function testIndex() {
$response = $this->get('/api/v1/foo');
// Assertions...
}
}
?>
Without the @Covers to explicitly link there is no way to see what this is covering. With it, my IDE shows this code in the usage list for index, I can quickly navigate to the code being tested though the docblock, the method doesn't show up as "unused", and if the method name changes but it passes (maybe in a unexpected way) the coverage report catches it and forces a review.
Testing controllers with coverage is pretty useful but a little weird but this sort of indirect call in testing can happen for a lot of reasons. Testing container interactions, code running in events, testing queue systems, etc.
So yeah, I think in an ideal world this wouldn't be useful. But in reality I find it incredibly useful.
from phpunit.
To me, allowing attributes for code coverage targeting to also be used on the test method level does not make sense.
from phpunit.
OK but... Drupal uses it and ever project I work on does so... other people are using it.
from phpunit.
@neclimdul maybe you could elaborate when it is useful, technically. Claiming "other people are using it" is not really an argument. Millions of flies can't be wrong, right?
I have never used it (maybe it would be useful in some projects I worked decade ago, but they had no test at all). I believe if you are following SRP then coverage on a class level is enough.
from phpunit.
Related Issues (20)
- Suppressed deprecations not included in baseline when ignoreSuppressionOfDeprecations is true HOT 1
- parameter names $expected, $actual don't make sense for assertLessThan() & friends
- Missing extensions in php 8.2: `ctype` and `tokenizer` HOT 2
- Various output buffer issues HOT 4
- Github template comment deprecated php.ini directive log_errors_max_len HOT 1
- PHP-Scoper 0.18.12 breaks PHAR for PHPUnit 8.5 on PHP 7.2 HOT 8
- Smarter defaults for the display of warnings, notices & co
- Mocked methods cannot be called from the original constructor of a partially mocked class HOT 4
- XML Configuration File Migrator does not remove `cacheDirectory` attribute from `<coverage>` element when migrating from PHPUnit 11.1 to PHPUnit 11.2 HOT 2
- Running tests with process isolation does not allow defining auto_prepend_file for child processes
- TestDox printer does not show previous exception
- PHP deprecations triggered within a Closure cause internal PHPUnit assert() to fail HOT 5
- TypeError: PHPUnit\Framework\TestSuiteIterator::current(): Return value must be of type PHPUnit\Framework\Test, null returned HOT 12
- Add option to limit export of arrays
- Change default value for `shortenArraysForExportThreshold` from `0` to `10`
- PHPUnit's error handler is not registered if Symfony is booted in a "before class" hook
- Doubled methods cannot be called from destructor on test double created by the return value generator HOT 14
- missing the upport to method Enum::cases() HOT 1
- Allow different file name to class name mapping HOT 2
- Include hint in deprecation notice for `--group`, `--exclude-group`, `--covers`, `--uses`, and `--test-suffix CLI` options HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpunit.