ParaTest Documentation

repository·7.x·Indexed 25 days ago

https://github.com/paratestphp/paratest

ParaTest is a tool for running PHPUnit tests in parallel to reduce execution time. It provides zero-configuration parallelization, automatic code coverage report combination, and environment variables like TEST_TOKEN and UNIQUE_TEST_TOKEN for isolating test resources. The tool supports integration with PHPStorm via a specialized wrapper and provides configurations for PCOV and xDebug coverage.

Tokens
1.7K
Snippets
7
Records
12
Agent score
81%

What's inside ParaTest

  1. ParaTest Limitations and Caveats

    7.x

    Due to limitations in the WrapperRunner and how PHPUnit searches for classes, the following are not supported:

    • Constants
    • Static methods
    • Static variables
    • Anything exposed by test classes consumed by other test classes (including via Reflection)

    Workaround: Move shared code into regular classes that are not themselves test classes.

  2. Use TEST_TOKEN for isolated test environments

    7.x

    ParaTest provides the TEST_TOKEN environment variable, which is guaranteed to have a unique value for every currently running test process. This is ideal for isolating resources like databases.

    Additionally, UNIQUE_TEST_TOKEN is available and is guaranteed to be unique both per run and per process.

    if (getenv('TEST_TOKEN') !== false) {  // Using ParaTest
        $dbname = 'testdb_' . getenv('TEST_TOKEN');
    } else {
        $dbname = 'testdb';
    }
  3. Configure Code Coverage with PCOV

    7.x

    If you use pcov and need to enable it specifically for your test run, use the --passthru-php option to pass the required PHP configuration directive.

    php -d pcov.enabled=1 vendor/bin/paratest --passthru-php="'-d' 'pcov.enabled=1'"
  4. Perform one-time initialization across parallel processes

    7.x

    Because ParaTest runs multiple processes, static variables only persist within a single process. A standard setUp() check for a static $initialized flag will run once per process, not once per test suite.

    To ensure an initialization step runs exactly once for the entire test invocation, use a filesystem lock to coordinate between processes.

    static bool $initialized = false;
    
    public function setUp(): void
    {
        if (! self::$initialized) {
            // We utilize the filesystem as shared mutable state to coordinate between processes
            touch('/tmp/test-initialization-lock-file');
            $lockFile = fopen('/tmp/test-initialization-lock-file', 'r');
    
            // Attempt to get an exclusive lock - first process wins
            if (flock($lockFile, LOCK_EX | LOCK_NB)) {
                // Since we are the single process that has an exclusive lock, we run the initialization
                self::initialize();
            } else {
                // If no exclusive lock is available, block until the first process is done with initialization
                flock($lockFile, LOCK_SH);
            }
    
            self::$initialized = true;
        }
    }
  5. Run ParaTest tests

    7.x

    After installation, the ParaTest binary is located at vendor/bin/paratest. You can run it to parallelize your PHPUnit tests. Use the --help option to view all available CLI options.

    By default, ParaTest parallelizes by TestCase. To parallelize by individual Test methods, use the --functional flag.

    vendor/bin/paratest
    vendor/bin/paratest --functional
  6. Integrate ParaTest with PHPStorm

    7.x

    To use ParaTest within PHPStorm's test runner:

    1. Ensure PHPUnit is already configured in PHPStorm.
    2. Go to Run -> Edit configurations....
    3. Add a new PHPUnit configuration and name it ParaTest.
    4. In Command Line -> Interpreter options, add ./vendor/bin/paratest_for_phpstorm.
    5. Add any specific ParaTest flags in the Test runner -> Test runner options section.

    This integration supports the Rerun failed tests and Toggle auto-test buttons. For code coverage, ensure coverage is already configured and working in PHPStorm for sequential runs.

  7. Run ParaTest using the provided Docker Compose configuration

    7.x

    The repository includes a docker-compose.yml file that allows you to run the ParaTest environment in a containerized PHP setup. To ensure correct file permissions within the container, you should provide USER_ID and GROUP_ID as environment variables during the build process. The configuration mounts the current directory to the working directory inside the container using the ${PWD} variable.

    services:
      php:
        build:
          context: .
          args:
            USER_ID: ${USER_ID}
            GROUP_ID: ${GROUP_ID}
        volumes:
          - .:${PWD}
        working_dir: ${PWD}
  8. Troubleshoot ParaTest failures

    7.x

    If tests fail, use the --verbose flag to get more information.

    When a sub-process fails, ParaTest outputs the exact command used. You can copy-paste this command into your terminal to debug it. Note that ParaTest normally uses --printer [...]\NullPhpunitPrinter to silence output; if you are debugging a command manually, you may need to remove this option to see the full PHPUnit output.

  9. Run ParaTest via the CLI

    7.x

    ParaTest is a command-line tool used to run tests in parallel. It is invoked via the bin/paratest executable. The tool requires Composer dependencies to be installed before it can run. If dependencies are missing, you must install them using Composer.

    To set up dependencies if they are not found, run:

    curl -s http://getcomposer.org/installer | php
    php composer.phar install
  10. Use paratest_for_phpstorm for PHPStorm integration

    7.x
    The bin/paratest_for_phpstorm script is a specialized CLI entrypoint designed to facilitate the integration of ParaTest with PHPStorm. It acts as a wrapper that translates arguments passed by PHPStorm into a format compatible with the standard paratest executable. This allows PHPStorm's test runner to leverage ParaTest's parallel execution capabilities.