Php:s2187: class extending abstract TestCase with test methods should not be reported

  • What language is this for? PHP

  • Which rule? php:s2187

  • Why do you believe it’s a false-positive/false-negative?
    When creating a configurable Abstract test case and then just using different sub-classes, class should not be reported as not containing any tests. When phpunit runs, all test methods from the hierarchy are also run.

    • SonarQube - which version? Enterprise Edition Version 9.9.4 (build 87374)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

abstract class AbstractTestCases extends TestCase
{
  abstract protected function createSystemUnderTest(): mixed;

  public function testInstanciatedSystemUnderTestPassesTest(): void
  {
    $sut = $this->createSystemUnderTest();

    // perform as many assertions as needed
    self::assertInstanceOf(MySpecificExpectation::class, $sut);
  }
}

class MyConcreteTest extends AbstractTestCases
{
  protected function createSystemUnderTest(): MyConcrete
  {
    return new MyConcrete();
  }
}

In the above example, SonarQube complains about MyConcreteTest not having any tests, which is false.

I tried to move testing methods inside a trait used in MyConcreteTest but the class is still marked as not having any tests which is clearly not the case.

Hey there.

I’m pretty confident this case was fixed in SonarQube v10.2 with SONARPHP-1401.

If you can upgrade, great! If not, you may want to mark the issue as a false-positive, remove it from your Quality Profile, or investigate other ways of ignoring issues on specific files.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.