ergebnis/phpunit-slow-test-detector
PHPUnit extension (Composer package and PHAR) that detects and reports slow tests during test runs. Configure a global maximum duration; when tests exceed it, the extension lists them with timings to help you spot and fix performance regressions.
composer require --dev ergebnis/phpunit-slow-test-detector
phpunit.xml:
Add the extension to your PHPUnit config based on your version (see README for version-specific syntax).
Example for PHPUnit 10+:
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
./vendor/bin/phpunit
The extension will now report slow tests (default threshold: 500ms).Identify slow tests in CI/CD pipelines:
devDependencies and configure it in phpunit.xml.- name: Run PHPUnit
run: ./vendor/bin/phpunit --stop-on-failure
Daily Development:
maximum-duration="500" to flag tests exceeding 500ms.<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
<php>
<server name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION" value="1000"/>
</php>
(Sets threshold to 1 second for broader detection.)CI/CD Integration:
maximum-duration="200").<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
<php>
<env name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION" value="200"/>
<env name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_COUNT" value="0"/> <!-- Fail on any slow test -->
</php>
Team Onboarding:
composer require --dev laravel-pint --dev ergebnis/phpunit-slow-test-detector
(Use tools like roave/security-advisories alongside.)phpunit.xml in tests/ and extend it for slow-test detection:
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
--parallel, configure thresholds per group:
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
</extensions>
<php>
<env name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION" value="300"/> <!-- Stricter for parallel -->
</php>
Version Mismatches:
<listeners>, while 7.x+ uses <extensions>.phpunit.xml per README.[PHPUnit\Configuration\Exception] Extension "Ergebnis\PHPUnit\SlowTestDetector\Extension" not found.
Threshold Overrides:
PHPUNIT_SLOW_TEST_DETECTOR_*) take precedence over XML config../vendor/bin/phpunit --debug
PHAR vs. Composer:
extensionsDirectory in phpunit.xml.Silent Failures: If slow tests aren’t reported, verify:
phpunit --debug output).500ms).<listeners>).Custom Output: Redirect slow-test logs to a file:
./vendor/bin/phpunit 2> slow-tests.log
Custom Thresholds: Override defaults via:
<php>
<env name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION" value="1000"/>
</php>
PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION=1000 ./vendor/bin/phpunit
Excluding Tests:
Use @skip annotations or filter test suites:
<testsuites>
<testsuite name="Fast Tests">
<directory>./tests/Unit</directory>
<exclude>*/Slow*</exclude>
</testsuite>
</testsuites>
CI-Specific Configs:
Use Laravel’s .env to dynamically set thresholds:
PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION=200
Load in phpunit.xml:
<php>
<env name="PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION" value="{env('PHPUNIT_SLOW_TEST_DETECTOR_MAXIMUM_DURATION', '500')}"/>
</php>
- name: Post Slow Test Alert
if: contains(steps.run-tests.outputs.stdout, 'Detected slow tests')
run: curl -X POST -H 'Content-type: application/json' --data '{"text":"Slow tests detected!"}' $SLACK_WEBHOOK
logs table) for trend analysis.xdebug to identify bottlenecks in slow tests:
XDEBUG_TRIGGER=1 ./vendor/bin/phpunit --filter SlowTest::testMethod
How can I help you explore Laravel packages today?