Summer Sale! All accounts are 50% off this week.

Smiffy's avatar

Parallel testing line coverage with PestPHP

Not sure if this is even possible but would like to find out if it is or not.

I'm wanting to run coverage over an application and include line coverage within this. I can do this however it takes an age so would like to leverage parallel testing. The command that works is:

php ./vendor/pestphp/pest/bin/pest --teamcity --coverage-html ./coverage --path-coverage --configuration /var/www/html/phpunit.xml

However adding --parallel into this results in the command unable to execute:

php ./vendor/pestphp/pest/bin/pest --teamcity --parallel --processes=6 --coverage-html ./coverage --path-coverage --configuration /var/www/html/phpunit.xml
The "--path-coverage" option does not exist.  
                                                
paratest [-m|--max-batch-size MAX-BATCH-SIZE] [--no-test-tokens] [--passthru-php PASSTHRU-PHP] [-p|--processes PROCESSES] [--runner RUNNER] [--tmp-dir TMP-DIR] [-v|--verbose] [--bootstrap BOOTSTRAP] [-c|--configuration CONFIGURATION] [--no-configuration] [--cache-directory CACHE-DIRECTORY] [--testsuite TESTSUITE] [--exclude-testsuite EXCLUDE-TESTSUITE] [--group GROUP] [--exclude-group EXCLUDE-GROUP] [--filter FILTER] [--process-isolation] [--strict-coverage] [--strict-global-state] [--disallow-test-output] [--dont-report-useless-tests] [--stop-on-defect] [--stop-on-error] [--stop-on-failure] [--stop-on-warning] [--stop-on-risky] [--stop-on-skipped] [--stop-on-incomplete] [--fail-on-incomplete] [--fail-on-risky] [--fail-on-skipped] [--fail-on-warning] [--order-by ORDER-BY] [--random-order-seed RANDOM-ORDER-SEED] [--colors [COLORS]] [--no-progress] [--display-incomplete] [--display-skipped] [--display-deprecations] [--display-errors] [--display-notices] [--display-warnings] [--teamcity] [--testdox] [--log-junit LOG-JUNIT] [--log-teamcity LOG-TEAMCITY] [--coverage-clover COVERAGE-CLOVER] [--coverage-cobertura COVERAGE-COBERTURA] [--coverage-crap4j COVERAGE-CRAP4J] [--coverage-html COVERAGE-HTML] [--coverage-php COVERAGE-PHP] [--coverage-text [COVERAGE-TEXT]] [--coverage-xml COVERAGE-XML] [--coverage-filter COVERAGE-FILTER] [--no-coverage] [--] [<path>]

ERROR: 1

Process finished with exit code 1

Line coverage does work when not using any parallel flags however. What gives?

Small addition - this is on Sail and I have increased the number of processes available with the env var PHP_CLI_SERVER_WORKERS=8

0 likes
1 reply
LaryAI's avatar
Level 58

It seems that the --path-coverage option is not compatible with Pest's parallel testing feature. One possible solution is to use PHPUnit's parallel testing feature instead, which is compatible with the --path-coverage option.

To do this, you can install PHPUnit and configure it to use Pest's test files. Here's an example configuration file (phpunit.xml) that you can use:

<phpunit bootstrap="./vendor/pestphp/pest/bin/pest.php">
    <testsuites>
        <testsuite name="Pest Tests">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./coverage" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
    </logging>
    <parallelize>
        <testsuite name="Pest Tests"/>
    </parallelize>
</phpunit>

This configuration file tells PHPUnit to use Pest's test files (./vendor/pestphp/pest/bin/pest.php as the bootstrap file), and to run the tests in parallel (<parallelize> tag). It also includes the --path-coverage option to generate line coverage reports (<logging> tag).

To run the tests, you can use the following command:

php ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml --processes 6

This command runs PHPUnit with the configuration file (--configuration option) and uses 6 processes (--processes option) for parallel testing.

Note that you may need to adjust the configuration file and command to fit your specific project structure and requirements.

Please or to participate in this conversation.