phpphpunitcode-coveragexdebugphp-code-coverage

Generate code coverage for a single test suite


I wish to determine which files are covered by each of my test suites separately. I am using PHPUnit 10.5 with Xdebug for code coverage on my PHP 8.1 project.

Here is what my phpunit.xml configuration file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    <testsuites>
        <testsuite name="example">
            <file>./ExampleTest.php</file>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory suffix=".php">../src</directory>
        </include>
    </source>>
    <coverage>
        <report>
            <html outputDirectory="html-coverage"/>
        </report>
    </coverage>
</phpunit>

I have done some research but haven't found anything helpful. Is there a way to achieve this?

Could it be that seeking code coverage for a single test suite is considered bad practice, and that's why no one tried to do it?

Any advice or insights would be greatly appreciated. Thank you!


Solution

  • I found an interesting workaround to achieve this.

    The XML coverage report includes a <coverage> element that lists the tests covering the class :

    <?xml version="1.0"?>
    <phpunit xmlns="https://schema.phpunit.de/coverage/1.0">
      <file name="Example.php" path="\path">
        <!--   ...   -->
        <coverage>
          <line nr="9">
            <covered by="someNamespace\ExampleTest::testSomething"/>
          </line>
        </coverage>
        <!--   ...   -->
      </file>
    </phpunit>
    

    My main goal is to identify which test suite covers which files. With this information, I should be able to make an algorithm that map the relationships between the elements, the test classes, and the test suites.

    This isn't the solution I was hoping for; I expected a simpler way to directly correlate test suites with their coverage.

    If anyone has a more efficient and cleaner way to do it, please share your solution! :)