DEV Community

Cover image for Parallel Testing with PHPUnit on CircleCI
Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Parallel Testing with PHPUnit on CircleCI

This article was originally published on bmf-tech.com.

Overview

This post discusses an approach to perform parallel testing with PHPUnit on CircleCI.

Prepare a Script to Generate PHPUnit Configuration File

#!/bin/sh

basePath="/foo/bar"
testFiles=$@

xmlFileStringData=""
for file in $testFiles; do
    xmlFileStringData="${xmlFileStringData}<file>${basePath}/${file}</file>\n"
done

testFileString="$xmlFileStringData"

template="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<phpunit colors=\"true\" stopOnFailure=\"false\" stopOnError=\"false\" failOnWarning=\"false\" stderr=\"true\" bootstrap=\"path/to/bootstrap\">
    <php>
     <ini name=\"memory_limit\" value=\"1G\"/>
     <ini name=\"realpath_cache_size\" value=\"1M\"/>
    </php>
    <testsuites>
        <testsuite name=\"Test Suite\">
            ${testFileString}
        </testsuite>
    </testsuites>
</phpunit>"

echo "$template" > "path/to/ci_phpunit.xml"
Enter fullscreen mode Exit fullscreen mode

Prepare a script like this to automatically generate the configuration file.

The reason for writing a shell script is that the tests are executed in a container, and due to using a convenience image in the CI job, there was no other suitable language.

Distribute Tests Across Containers for Parallelization on CircleCI

With the script prepared earlier as generate_phpunit.sh, you can prepare for parallelization with the following script.

circleci tests glob "path/to/testdir/**/*.php" | circleci tests split | xargs sh +x generate_phpunit.sh
Enter fullscreen mode Exit fullscreen mode

After that, by specifying the generated configuration file during test execution, tests can be executed across multiple containers, enabling parallelization.

path/to/phpunit -c path/to/ci_phpunit.xml
Enter fullscreen mode Exit fullscreen mode

An example when testing with docker-compose.

docker-compose -f docker/docker-compose.test.yml run test ash -c "path/to/phpunit -c path/to/ci_phpunit.xml"
Enter fullscreen mode Exit fullscreen mode

Thoughts

This was something I actually tried in a work setting, but it turned out there were dependencies on the execution order between tests, making it difficult to easily achieve parallel test execution...

First, we need to address the dependencies...

References

Top comments (0)