Provides facilities for detecting slow tests in phpunit/phpunit.

1.0.0 2023-02-03 14:37 UTC

This package is auto-updated.

Last update: 2023-02-07 12:03:13 UTC


Integrate Merge Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads

Provides an extension for detecting slow tests in phpunit/phpunit.



composer require --dev ergebnis/phpunit-slow-test-detector


Activating the extension

This extension provides three event subscribers for phpunit/phpunit:

  • Subscriber\TestPreparedSubscriber
  • Subscriber\TestPassedSubscriber
  • Subscriber\TestSuiteFinishedSubscriber

These subscribers depend on the following:

  • a TimeKeeper for keeping test prepared and passed times
  • a MaximumDuration
  • a Collector\Collector for collecting slow tests
  • a Reporter\Reporter for reporting slow tests

To activate this extension, you need to register these subscribers with the event system of phpunit/phpunit. As of the moment, this is only possible with a bootstrap.php script:



require_once __DIR__ . '/vendor/autoload.php';

use Ergebnis\PHPUnit\SlowTestDetector;
use PHPUnit\Event;

$timeKeeper = new SlowTestDetector\TimeKeeper();

Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestPreparedSubscriber($timeKeeper));

$maximumDuration = SlowTestDetector\MaximumDuration::fromMilliseconds(500);

$collector = new SlowTestDetector\Collector\DefaultCollector();

Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestPassedSubscriber(

$maximumCount = SlowTestDetector\MaximumCount::fromInt(10);

$reporter = new SlowTestDetector\Reporter\DefaultReporter(
    new SlowTestDetector\Formatter\ToMillisecondsDurationFormatter(),

Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestRunnerExecutionFinishedSubscriber(

❗ Currently, this is a bit verbose. @sebastianbergmann, @theseer, and I are going to meet to talk about how we can improve this.

Configuring maximum duration per test case

When necessary, you can configure the maximum duration for a test with a @slowThreshold annotation in the DocBlock.

This example configures the maximum duration for a single test to 5.000 ms:



use PHPUnit\Framework;

final class ExtraSlowTest extends Framework\TestCase
     * @slowThreshold 5000
    public function testExtraExtraSlow(): void
        // ...

Running tests

When you have activated the extension, you can run your tests as usually:


When the extension has detected slow tests, it will report them:

PHPUnit 10.0-dev by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.14
Configuration: test/Example/phpunit.xml
Random Seed:   1611649366

.....                                                                                                                                                                                                                                                                                                             5 / 5 (100%)

Detected 4 tests that took longer than expected.

1,012 ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::testSleeperSleepsOneSecond
  755 ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::testSleeperSleepsThreeQuartersOfASecond
  503 ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\Example\SleeperTest::testSleeperSleepsHalfASeconds

There is one additional slow test that is not listed here.

Time: 00:02.563, Memory: 10.00 MB

OK (5 tests, 5 assertions)


Please have a look at


Please have a look at

Code of Conduct

Please have a look at


This package is licensed using the MIT License.

Please have a look at


This project is inspired by johnkary/phpunit-speedtrap.

Curious what I am building?

📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.