PHPUnit SpeedTrap

PHPUnit SpeedTrap

PHPUnit Speedtrap is a package by John Kary that reports on slow-running tests in your PHPUnit test suite. This package is inspired by Ruby RSpec’s --profile option that displays feedback about slow tests:

SpeedTrap reports on slow-running PHPUnit tests right in the console.

Many factors affect test execution time. A test not properly isolated from variable latency (database, network, etc.) and even basic load on the test machine will cause test execution times to fluctuate.

From the project’s readme, here’s an example of what you can expect:

I am not super familiar with tapping into PHPUnit, so I enjoyed diving into the package’s source code and learning how PHPUnit listeners work. In a phpunit.xml file you can enable this package with the following XML:

<listeners>
    <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
</listeners>

A more extensive example includes customizable options for defining the “slowTreshold” and “reportLength”. You can customize what your test suite considers a “slow” test defined in milliseconds:

<listeners>
    <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener">
        <arguments>
            <array>
                <element key="slowThreshold">
                    <integer>500</integer>
                </element>
                <element key="reportLength">
                    <integer>5</integer>
                </element>
            </array>
        </arguments>
    </listener>
</listeners>

Along with the listener, the package demonstrates using an annotation to override these configuration options on the per-test level:

/**
 * @slowThreshold 5000
 */
public function testLongRunningProcess()
{
    // Code that takes a longer time to execute
}

You can learn more about the PHPUnit Speedtrap package at johnkary/phpunit-speedtrap and also check out these tips on speeding up your PHPUnit tests


Filed in: Laravel Packages


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Laravel / Vue.JS Developer - Join an amazing team!
Remote
JTR Solutions
Mid / Sen. Software Engineer
Clearwater, FL
ShineOn
Remote PHP / Laravel Developer
Remote
SpringboardVR
Senior PHP/Laravel Developer: Your Dream Work Environment
Remote
iPhone Photography School
Senior Laravel Developer
Leidseplein, Amsterdam
Orderchamp.com
PHP Developer
Remote
X-Team
Senior Laravel Developer (Canada and India)
London, Ontario, Canada
Factory Bucket Inc.