ergebnis / phpunit-slow-test-detector
Provides facilities for detecting slow tests in phpunit/phpunit.
Installs: 251 554
Dependents: 27
Suggesters: 0
Security: 0
Stars: 63
Watchers: 5
Forks: 1
Open Issues: 1
Requires
- php: ~8.1.0 || ~8.2.0
- phpunit/phpunit: ^10.1.3
Requires (Dev)
- ergebnis/composer-normalize: ^2.31.0
- ergebnis/data-provider: ^1.3.0
- ergebnis/license: ^2.1.0
- ergebnis/php-cs-fixer-config: ^5.7.0
- fakerphp/faker: ^1.22.0
- psalm/plugin-phpunit: ~0.18.4
- rector/rector: ~0.16.0
- vimeo/psalm: ^5.12.0
Suggests
- phpunit/phpunit: ^10.0.1
This package is auto-updated.
Last update: 2023-09-27 07:29:53 UTC
README
Provides an extension for detecting slow tests in phpunit/phpunit
.
Installation
Composer
Run
composer require --dev ergebnis/phpunit-slow-test-detector
to install ergebnis/phpunit-slow-test-detector
as a composer package.
Phar
Download phpunit-slow-test-detector.phar
from the latest release.
Usage
Bootstrapping the extension as a composer package
To bootstrap the extension as a composer package, adjust your phpunit.xml
configuration file:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" > + <extensions> + <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/> + </extensions> <testsuites> <testsuite name="unit"> <directory>test/Unit/</directory> </testsuite> </testsuites> </phpunit>
Bootstrapping the extension as a PHAR
To bootstrap the extension as a PHAR, adjust your phpunit.xml
configuration file and configure the extensionsDirectory
attribute of the <phpunit>
element:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" + extensionsDirectory="directory/where/you/saved/the/extension/phars" > + <extensions> + <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/> + </extensions> <testsuites> <testsuite name="unit"> <directory>test/Unit/</directory> </testsuite> </testsuites> </phpunit>
Configuring the extension
You can configure the extension with the following parameters in your phpunit.xml
configuration file:
maximum-count
, anint
, the maximum count of slow test that should be listed, defaults to10
maximum-duration
, anint
, the maximum duration in milliseconds for all tests, defaults to500
The following example configures the maximum count of slow tests to three, and the maximum duration for all tests to 250 milliseconds:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" > <extensions> - <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/> + <bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"> + <parameter name="maximum-count" value="3"/> + <parameter name="maximum-duration" value="250"/> + </bootstrap> </extensions> <testsuites> <testsuite name="unit"> <directory>test/Unit/</directory> </testsuite> </testsuites> </phpunit>
Configuring the maximum duration per test case
You can configure the maximum duration for a single test with a @maximumDuration
(or @slowThreshold
) annotation in the DocBlock.
The following 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 { /** * @maximumDuration 5000 */ public function testExtraExtraSlow(): void { // ... } /** * @slowThreshold 4000 */ public function testAlsoQuiteSlow(): 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.0 by Sebastian Bergmann and contributors. Runtime: PHP 8.1.0 Configuration: test/EndToEnd/Default/phpunit.xml Random Seed: 1676103726 ............. 13 / 13 (100%) Detected 11 tests that took longer than expected. 1. 1.604 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#9 2. 1.505 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#8 3. 1.403 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#7 4. 1.303 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#6 5. 1.205 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 6. 1.103 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 7. 1.005 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 8. 0.905 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 9. 0.805 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 10. 0.705 (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#0 There is 1 additional slow test that is not listed here. Time: 00:12.601, Memory: 8.00 MB OK (13 tests, 13 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 up to?
Follow me on Twitter!