If you’ve been writing PHPUnit tests for long, you’ve probably run into a time when a test works 90% of the time, but every now and then it throws an unexpected error or failure. If it happens only rarely, you might just get around it by re-running your test suite, but if you’ve got a large test suite or intermittent failures become really common, you probably need to address the issue. Here’s a quick tip for debugging tests like this.
If you want to run your PHPUnit tests until they fail, type this into your command line and hit
while (phpunit); do :; done
This simple bash script uses a while loop to run the command within parentheses over and over again until it receives an error or failure response. You can add any PHPUnit flags you want to the command:
while (phpunit --filter thatOnePeskyFailingTest); do :; done
while (phpunit --testsuite suiteName); do :; done
Your bash script can be more complicated than one command. You could use a
&& to chain two commands together. For example, I wanted to delete the contents of my Laravel application’s log file and then run the acceptance tests in my app until they failed:
while (> storage/logs/laravel.log && phpunit --testsuite acceptance); do :; done
Each time, the script cleared out my logs before running the test, which allowed me to use the logs to debug the issue.
If you ever want to break out of a never-ending test loop, just hold
control and press
c (on Mac).
Bash scripting isn’t something I choose to do often, but at times it’s the best tool for the job. If you are interested in more posts about testing, read my other post on testing with multiple versions of PHP or follow me on Twitter.
In this book, PHP developers will learn everything they need to know to start building their applications on Docker, including:
You can buy this book on Leanpub today.