Pest - Targeted multiple times by the same Cover attribute
Not sure exactly with what version this came with, but upgraded to v4 of pest and run my test.
Getting quite some warnings.
Class App\Http\Controllers\ProfileController is targeted multiple times by the same "Covers" attribute```
Can't find much about it, any ideas?
ProfileTest.php
Class App\Http\Controllers\ProfileController is targeted multiple times by the same "Covers" attribute
means that the #[Covers(...)] attribute is being applied more than once for the same class or method in your tests. Pest (and PHPUnit) uses the #[Covers] attribute to indicate which classes or methods are being tested for code coverage reporting. If you annotate multiple tests or test classes with the same #[Covers] for the same target, Pest will emit this warning.
How to Fix:
Check for Duplicate Covers Attributes
Open your ProfileTest.php and look for multiple usages of the same #[Covers(App\Http\Controllers\ProfileController::class)] attribute, either at the class level or on individual test methods.
For example, you might have:
#[Covers(App\Http\Controllers\ProfileController::class)]
class ProfileTest extends TestCase
{
#[Covers(App\Http\Controllers\ProfileController::class)]
public function test_profile_update()
{
// ...
}
}
Here, the #[Covers] attribute is applied both to the class and the method, which is redundant.
Remove Redundant Attributes
Only apply the #[Covers] attribute once per target. If you want all tests in the class to cover the controller, put it on the class. If only specific methods cover it, put it only on those methods.
Example:
#[Covers(App\Http\Controllers\ProfileController::class)]
class ProfileTest extends TestCase
{
public function test_profile_update()
{
// ...
}
}
Or, if only some methods cover it:
class ProfileTest extends TestCase
{
#[Covers(App\Http\Controllers\ProfileController::class)]
public function test_profile_update()
{
// ...
}
}
Check for Multiple Test Files
If you have multiple test files all using the same #[Covers] for the same class, Pest will warn you. Consider if you need the attribute in all those files, or if you can organize your tests differently.
Summary:
Remove duplicate #[Covers] attributes targeting the same class or method in your test files. Apply them only where necessary.