PHPUnit find slow tests

When optimizing your testsuite it can be handy do find out how long each test takes to run. It would be nice to have a list of and how many seconds they took, like this:

 $ phpunit-report-runtime
 0.29307007789612, "VCR\\Util\\SoapClientTest::testDoRequestHookDisabled"
 0.16475319862366, "VCR\\CassetteTest::testRecordAndPlaybackRequest"
 0.092710018157959, "VCR\\Util\\SoapClientTest::testDoRequest"
 0.031861782073975, "VCR\\LibraryHooks\\SoapTest::testShouldInterceptCallWhenEnabled"
 0.026772022247314, "VCR\\LibraryHooks\\AbstractFilterTest::testRegisterAlreadyRegistered"

One solution is to write your own test listener and add it to your phpunit.xml. If you don’t like to modify your code, it is possible to parse out the time from PHPUnit’s JSON test result. Put the following alias in your shells initialization script (like .bashrc).

alias phpunit-report-runtime="phpunit --log-json php://stdout \
    | awk '\$NF ~ '/,/' && \$1 ~ /\"(test|time)\"/' \
    | cut -d: -f2- \
    | sed \"N;s/\n/--/\"  \
    | sed \"s/,//\"   \
    | awk 'BEGIN{FS=\"--\"}; {print \$2 \$1}' | sort -r \
    | head -n 5"

Running the alias phpunit-report-runtime in your project prints the top 5 slowest tests with the slowest test at the top. By changing head -n 5 you can adjust the amount of tests printed.

Related Posts

Learning How to Learn – Online Course Notes

My notes for the online course “Learning How to Learn”.

Why the Elixir language has great potential

Elixir enables creating fast, reliable and concurrent modern applications with high productivity

Tips for making screencasts for presentations

As an alternative to live demos you can play screen casts during your tech talk.

Trust and Integrity

My notes for the Speed of Trust by FranklinCovey.

NodeJS: Factory Function Pattern, Dependency Registry and Hot Reloading

This article shows how to combine Factory Functions with a rapid feedback loop using hot reloading.

Better back swiping with the React Native Navigator

Swiping back works only close to the edge by default which can be hard to accomplish. It is easy to optimize this.

How to keep plugins when updating PhpStorm

When updating PhpStorm my plugins seemed to be gone, but you can easily move them over.

Command duration in fish shell

Display how long the last command took and notify when a long running command is finished.

ES6 in Node and in your Browser with Traceur

How to use Google traceur to transpile ES6 javascript to ES5.

Remote debugging for PHP cli scripts

Enable remote debugging for your console PHP scripts without much fuzz.