brianium / paratest
Parallel testing for PHP
Installs: 62 623 655
Dependents: 662
Suggesters: 32
Security: 0
Stars: 2 316
Watchers: 39
Forks: 229
Open Issues: 2
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0
- ext-dom: *
- ext-pcre: *
- ext-reflection: *
- ext-simplexml: *
- fidry/cpu-core-counter: ^1.2.0
- jean85/pretty-package-versions: ^2.0.6
- phpunit/php-code-coverage: ^11.0.7
- phpunit/php-file-iterator: ^5.1.0
- phpunit/php-timer: ^7.0.1
- phpunit/phpunit: ^11.4.1
- sebastian/environment: ^7.2.0
- symfony/console: ^6.4.11 || ^7.1.5
- symfony/process: ^6.4.8 || ^7.1.5
Requires (Dev)
- ext-pcov: *
- ext-posix: *
- doctrine/coding-standard: ^12.0.0
- phpstan/phpstan: ^1.12.6
- phpstan/phpstan-deprecation-rules: ^1.2.1
- phpstan/phpstan-phpunit: ^1.4.0
- phpstan/phpstan-strict-rules: ^1.6.1
- squizlabs/php_codesniffer: ^3.10.3
- symfony/filesystem: ^6.4.9 || ^7.1.5
- 7.x-dev
- v7.6.0
- v7.5.7
- v7.5.6
- v7.5.5
- v7.5.4
- v7.5.3
- v7.5.2
- v7.5.1
- v7.5.0
- 7.4.x-dev
- v7.4.8
- v7.4.7
- v7.4.6
- v7.4.5
- v7.4.4
- v7.4.3
- v7.4.2
- v7.4.1
- v7.4.0
- v7.3.1
- v7.3.0
- v7.2.9
- v7.2.8
- v7.2.7
- v7.2.6
- v7.2.5
- v7.2.4
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.5
- v7.1.4
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- 6.x-dev
- v6.11.1
- v6.11.0
- v6.10.1
- v6.10.0
- v6.9.1
- v6.9.0
- v6.8.1
- v6.8.0
- v6.7.0
- v6.6.5
- v6.6.4
- v6.6.3
- v6.6.2
- v6.6.1
- v6.6.0
- v6.5.1
- v6.5.0
- v6.4.4
- v6.4.3
- v6.4.2
- v6.4.1
- v6.4.0
- v6.3.3
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.0
- v6.1.2
- v6.1.1
- v6.1.0
- v6.0.0
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.2.0
- 4.2.0-beta.1
- 4.1.1-alpha.2
- 4.1.1-alpha.1
- 4.1.0
- 4.0.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.0
- 2.2.0
- 2.1
- 2.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.3
- 0.13.2
- 0.13.1
- 0.13.0
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.1
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.3
- 0.7.2
- 0.7.1
- v0.7.0
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.0
- dev-renovate/all-minor-patch
This package is auto-updated.
Last update: 2024-11-18 22:16:35 UTC
README
The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest
in your project and
start using it with no additional bootstrap or configurations!
Benefits:
- Zero configuration. After the installation, run with
vendor/bin/paratest
to parallelize by TestCase orvendor/bin/paratest --functional
to parallelize by Test. That's it! - Code Coverage report combining. Run your tests in N parallel processes and all the code coverage output will be combined into one report.
Installation
To install with composer run the following command:
composer require --dev brianium/paratest
Versions
Only the latest version of PHPUnit is supported, and thus only the latest version of ParaTest is actively maintained.
This is because of the following reasons:
- To reduce bugs, code duplication and incompatibilities with PHPUnit, from version 5 ParaTest heavily relies on PHPUnit
@internal
classes - The fast pace both PHP and PHPUnit have taken recently adds too much maintenance burden, which we can only afford for the latest versions to stay up-to-date
Usage
After installation, the binary can be found at vendor/bin/paratest
. Run it
with --help
option to see a complete list of the available options.
Test token
The TEST_TOKEN
environment variable is guaranteed to have a value that is different
from every other currently running test. This is useful to e.g. use a different database
for each test:
if (getenv('TEST_TOKEN') !== false) { // Using ParaTest $dbname = 'testdb_' . getenv('TEST_TOKEN'); } else { $dbname = 'testdb'; }
A UNIQUE_TEST_TOKEN
environment variable is also available and guaranteed to have a value that is unique both
per run and per process.
Code coverage
The cache is always warmed up by ParaTest before executing the test suite.
PCOV
If you have installed pcov
but need to enable it only while running tests, you have to pass thru the needed PHP binary
option:
php -d pcov.enabled=1 vendor/bin/paratest --passthru-php="'-d' 'pcov.enabled=1'"
xDebug
If you have xDebug
installed, activating it by the environment variable is enough to have it running even in the subprocesses:
XDEBUG_MODE=coverage vendor/bin/paratest
Initial setup for all tests
Because ParaTest runs multiple processes in parallel, each with their own instance of the PHP interpreter, techniques used to perform an initialization step exactly once for each test work different from PHPUnit. The following pattern will not work as expected - run the initialization exactly once - and instead run the initialization once per process:
private static bool $initialized = false; public function setUp(): void { if (! self::$initialized) { self::initialize(); self::$initialized = true; } }
This is because static variables persist during the execution of a single process.
In parallel testing each process has a separate instance of $initialized
.
You can use the following pattern to ensure your initialization runs exactly once for the entire test invocation:
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; } }
Troubleshooting
If you run into problems with paratest
, try to get more information about the issue by enabling debug output via
--verbose --debug
.
When a sub-process fails, the originating command is given in the output and can then be copy-pasted in the terminal
to be run and debugged. All internal commands run with --printer [...]\NullPhpunitPrinter
which silence the original
PHPUnit output: during a debugging run remove that option to restore the output and see what PHPUnit is doing.
Caveats
- Constants, static methods, static variables and everything exposed by test classes consumed by other test classes
(including Reflection) are not supported. This is due to a limitation of the current implementation of
WrapperRunner
and how PHPUnit searches for classes. The fix is to put shared code into classes which are not tests themselves.
Integration with PHPStorm
ParaTest provides a dedicated binary to work with PHPStorm; follow these steps to have ParaTest working within it:
- Be sure you have PHPUnit already configured in PHPStorm: https://www.jetbrains.com/help/phpstorm/using-phpunit-framework.html#php_test_frameworks_phpunit_integrate
- Go to
Run
->Edit configurations...
- Select
Add new Configuration
, select thePHPUnit
type and name itParaTest
- In the
Command Line
->Interpreter options
add./vendor/bin/paratest_for_phpstorm
- Any additional ParaTest options you want to pass to ParaTest should go within the
Test runner
->Test runner options
section
You should now have a ParaTest
run within your configurations list.
It should natively work with the Rerun failed tests
and Toggle auto-test
buttons of the Run
overlay.
Run with Coverage
Coverage with one of the available coverage engines must already be configured in PHPStorm and working when running tests sequentially in order for the helper binary to correctly handle code coverage
For Contributors: testing ParaTest itself
Before creating a Pull Request be sure to run all the necessary checks with make
command.