This post may now be outdated. See the johnkary/phpunit-speedtrap project on GitHub for the most recent instructions.
All developers should strive to write "fast tests," and "fast" will be different for each developer, language and software stack. Traditionally, unit tests should be very fast, a few milliseconds or faster, while functional tests and end-to-end tests will run a bit longer, maybe a few seconds.
As your test suite grows, you can expect it to run longer. But if you're not careful, some slower tests can creep in and disproportionately increase your suite's total execution time.
SpeedTrap is a PHPUnit Listener that reports on slow-running tests in your PHPUnit test suite console output.
Many factors affect test execution time. A test not properly isolated from variable latency (database, network, etc.) and even basic load on your test machine will cause test times to fluctuate.
SpeedTrap helps you identify slow tests but cannot tell you why those tests are slow. For that you should check out something like PHPUnit_Listener_XHProf to help identify specifically which methods in your call stack are slow.
SpeedTrap is installable via Composer and should be added as a dev dependency:
Enabled it with default configuration by adding the following to your test suite's phpunit.xml
file:
Now run your test suite as normal. If tests run that exceed the slowness threshold (500ms by default), SpeedTrap will report on them in the console after the suite completes.
SpeedTrap has two configurable parameters:
These configuration parameters are set in phpunit.xml
when adding the listener:
<span class="nt"><listeners></span>
<span class="nt"><listener</span> <span class="na">class=</span><span class="s">"JohnKary\PHPUnit\Listener\SpeedTrapListener"</span><span class="nt">></span>
<span class="nt"><arguments></span>
<span class="nt"><array></span>
<span class="nt"><element</span> <span class="na">key=</span><span class="s">"slowThreshold"</span><span class="nt">></span>
<span class="nt"><integer></span>500<span class="nt"></integer></span>
<span class="nt"></element></span>
<span class="nt"><element</span> <span class="na">key=</span><span class="s">"reportLength"</span><span class="nt">></span>
<span class="nt"><integer></span>10<span class="nt"></integer></span>
<span class="nt"></element></span>
<span class="nt"></array></span>
<span class="nt"></arguments></span>
<span class="nt"></listener></span>
<span class="nt"></listeners></span>
</phpunit>
This allows you to set your own criteria for "slow" tests, and how many you care to know about.
SpeedTest was inspired by Rspec's -p
option that displays feedback about slow tests.
phpunit-speedtrap is available under the MIT License.
comments powered by Disqus