Run Laravel Artisan Tests directly in PhpStorm test view

| 3 min read

One of the teams I work with uses Laravel and enjoys running tests via PhpStorm. Unfortunately, it’s not possible to run Laravel tests using php artisan test directly within the IDE’s test interface. This forces us to run tests slower than with the CLI because they don’t run in parallel.

You might think it’s possible to run Laravel tests using Paratest directly in PhpStorm, but this doesn’t work either due to how the database cleanup system is implemented—at least, that was the case the last time I tried.

Since we prefer PhpStorm’s interface for running our tests, I wondered if it was possible to run Laravel tests directly inside PhpStorm’s test interface, and it turns out that it is—with a bit of a hack.

Get the Wrapper

Copy and paste the following code into a file named phpunit somewhere in your project. In my case, I put it in tooling/PhpstormLaravelTest/phpunit. The file needs to be named phpunit because Paratest, used when you want to run multiple test classes at once (from the same directory, for instance), checks that specific filename.

#!/usr/bin/env php  
<?php declare(strict_types=1);

// This will need to be named `/phpunit` if you want to be able to use that tool alongside paratest for phpstorm,
// for instance when you are running all tests in a directory.
// The best thing I came up with so far is to create a directory `PhpStormForLaravelTest` and create that file inside.
echo "👋 Laravel Wrapper for Phpstorm PHPUnit 10.5.15-Fake. For the real PhpUnit and Paratest versions see below.\n";

// Artisan test doesn’t have a --filter argument, we need to use phpunit when running only part of the tests.
if (in_array('--filter', $argv)) {
$command = 'php vendor/phpunit/phpunit/phpunit';

// Add single quote for every argument to avoid escaping issues that prevent
$args = array_slice($argv, 1);
$args = array_map(fn($a) => "'${a}'", $args);

$command .= ' ' . implode(' ', $args);
} else {
$command = 'php artisan test --parallel --teamcity';
}

echo "🔍 Running {$command}\n";

passthru($command);

As you can see, this is clearly a hack. There is no guarantee it will work in every situation. It’s a wrapper on top of a wrapper that uses a library wrapping the testing framework. You have to account for Paratest specifics (naming the file /phpunit) and artisan test specifics (switching to PHPUnit when the --filter option is used because otherwise it doesn’t work).

What this script does is simple: when there are no options, it uses php artisan test --parallel to execute tests as fast as possible, while asking for TeamCity-compatible output that PhpStorm can parse and display in its test view. When you’re trying to run a single test or test class, PhpStorm will use the --filter option, and in that case, we redirect to PHPUnit.

A big hack, but one that serves us well.

Configure PhpStorm

Once installed, you need to configure it in PhpStorm.

In PhpStorm settings, go to PHP > Test Frameworks.
PhpStorm test frameworks setting window

Add a new test runner by clicking the + and selecting PHPUnit by Remote Interpreter.
Interpreter type window

Select the interpreter that will run the tests. If you already have a test runner configured to use that interpreter, you’ll have to remove it, as PhpStorm only allows one test runner per interpreter.

Interpreter selection window

Do you speak French and want to stop hating your tests ?

I've created a course to help developers to improve their automated tests.

I share ideas and technics to improve slow, flaky, failing for unexpected reason and hard to understand tests until they become tests we take joy to work with !

But you'll need to understand French...

Configure the interpreter to use a .phar file and point to the wrapper file with the correct mapping. It should look something like /var/www/html/phpstorm-laravel-test-wrapper.php. Click the reload button to check if it works. You should see a fake PHPUnit version displayed.

Test framework settings

In your test run settings, ensure you’re using the correct PHP interpreter—in our case, laravel.test.

Run configuration setting window

And there you go! Enjoy running your tests with speed and ease in PhpStorm 🏎️.

Conclusion

Using PhpStorm’s test interface with Laravel offers a much better developer experience, but it requires some clever configuration hacks. By setting up this wrapper, you can enjoy the best of both worlds: the speed of php artisan test --parallel and the flexibility of PHPUnit for individual tests.

If you’re looking to streamline your team’s testing process or need help with Agile technical coaching, I’m here to assist. Let’s discuss how we can improve your workflow. Feel free to book a slot with me via my open calendar.