ergebnis / phpunit-slow-test-detector
Provides facilities for detecting slow tests in phpunit/phpunit.
Requires
- php: ~8.1.0 || ~8.2.0
- phpunit/phpunit: ^10.0.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.29.0
- ergebnis/data-provider: ^1.3.0
- ergebnis/license: ^2.1.0
- fakerphp/faker: ^1.21.0
- rector/rector: ~0.15.11
- vimeo/psalm: ^5.6.0
This package is auto-updated.
Last update: 2023-02-07 12:03:13 UTC
README
Provides an extension for detecting slow tests in phpunit/phpunit
.
Installation
Run
composer require --dev ergebnis/phpunit-slow-test-detector
Usage
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:
<?php declare(strict_types=1); 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( $maximumDuration, $timeKeeper, $collector )); $maximumCount = SlowTestDetector\MaximumCount::fromInt(10); $reporter = new SlowTestDetector\Reporter\DefaultReporter( new SlowTestDetector\Formatter\ToMillisecondsDurationFormatter(), $maximumDuration, $maximumCount ); Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestRunnerExecutionFinishedSubscriber( $collector, $reporter ));
❗ 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:
<?php declare(strict_types=1); 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:
vendor/bin/phpunit
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)
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
Code of Conduct
Please have a look at CODE_OF_CONDUCT.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
Credits
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.