How to run PHPUnit tests in a random order

Published on

Sometimes you can end up in a situation where your tests only pass if they are run in a specific order. This means that if you change one test, another test can fail. Not great.

Each test should start with a clean slate, it shouldn't depend on left-overs from previous tests. Tests that depend on each other can hard to notice because you usually run your tests in the same order.

An effective way to detect this problem in your testsuite is by shuffling your tests. You can run your tests in a random order using this PHPUnit flag:

# For unit tests:
vendor/bin/phpunit --order-by=random

# For Laravel Dusk tests:
php artisan dusk --order-by=random