Home » Php » php – Generate coverage only of what was just tested

php – Generate coverage only of what was just tested

Posted by: admin July 12, 2020 Leave a comment

Questions:

Everytime I run unit tests of one class or a whole folder, phpunit generate coverage for the whole system, because that’s configured in phpunit.xml.
This is bad because it takes longer and exhausts PHP’s memory.

My phpunit.xml

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "true"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "false"
    stopOnFailure               = "false"
    syntaxCheck                 = "false"
    bootstrap                   = "Bootstrap.php" >

    <testsuites>
        <testsuite name="Application Module Suite Test">
            <directory>./Module1Test</directory>
            <directory>./Module2Test</directory>
            <directory>./Module3Test</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>../module/Module1</directory>
            <directory>../module/Module2</directory>
            <directory>../module/Module3</directory>
        </whitelist>
    </filter>

</phpunit>

Is there a way to generate coverage of only what I’m testing right now, dynamically?

Example
For the command below I’d like to generate coverage for Controller/ExampleController.php path only.

phpunit Controller/ExampleController.php --coverage-html ~/Desktop/tests

I’m using PHPUnit 4.8 and 3.7, Sublime Text Editor and the application is using Zend Framework 2.

How to&Answers:

From the PHPUnit version 5.6 manual:

The @covers annotation (see Table B.1) can be used in the test code to specify which method(s) a test method wants to test. If provided, only the code coverage information for the specified method(s) will be considered. Example 11.2 shows an example.

See this link for an example: https://phpunit.de/manual/current/en/code-coverage-analysis.html#code-coverage-analysis.specifying-covered-methods.examples.BankAccountTest.php

Can this be useful for your scenario?

Answer:

You’ll need to set the addUncoveredFilesFromWhitelist parameter to false:

<filter>
    <whitelist addUncoveredFilesFromWhitelist="false">
        <directory>../module/Module1</directory>
        <directory>../module/Module2</directory>
        <directory>../module/Module3</directory>
    </whitelist>
</filter>

See this answer for an explanation on the addUncoveredFilesFromWhitelist and processUncoveredFilesFromWhitelist parameters.

Answer:

add processUncoveredFilesFromWhitelist parameter to True as in https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.whitelisting-files

<filter>
    <whitelist processUncoveredFilesFromWhitelist="true">
        <directory>../module/Module1</directory>
        <directory>../module/Module2</directory>
        <directory>../module/Module3</directory>
    </whitelist>
</filter>