Run and debug PHPUnit tests in PhpStorm

It's possible to run and debug single tests as well as tests from entire files and folders from PhpStorm. Here, we'll explain how to do the setup and the steps you need to follow to do this.

Run PHPUnit test

To run a PHPUnit test, you first need to create a CLI interpreter. To do this, open PhpStorm and then follow the steps below:

  1. Go to Settings, then Language & Frameworks, and then PHP
  2. Click the more icon (...) on the CLI interpreter option

You'll now see a new screen for CLI Interpreters.

  1. Click the add icon (+) and select the option From Docker, Vagrant, VM, ...
  2. Select Configure Remote PHP Interpreter
  3. Click the add icon (+) to create a new SSH configuration
  4. Enter the Host, User name, and Password with the details from your developer sandbox

Once you've done this, you should be able to see your new CLI interpreter:

After clicking the OK button, you'll be taken back to the PHP config screen. There, you can click on the folder icon and specify the mapping between your Local Path and the Remote Path /var/www/frontastic:

Set up a new test framework

To set up a new test framework in PhpStorm, you'll need to follow the steps below:

  1. Go to Settings, then Language & Frameworks, then PHP, and select Test Frameworks
  2. Click the add icon (+) and select the PHPUnit by Remote Interpreter

This will open a new window.

  1. Select the CLI interpreter that you have created and click OK
  2. Once the new test framework is created, you should specify the Path to script with /var/www/frontastic/<project_folder>/vendor/autoload.php:

Set up PHPUnit test suit

To set up a test suit in PhpStorm, you'll need to follow the steps below:

  1. Go to Menu, then Run, and select Edit configuration
  2. Click the add icon (+) and select PHPUnit
  3. Input a name, for example, Test suit, and specify your local directory where your PHPUnit test will live
  4. Click OK

You should now be able to run your PHPUnit test within PhpStorm.

Debug PHPUnit test

In order to debug the PHPUnit test, you'll need to update the PhpStorm config. To do this:

  1. Go to Settings, then Language & Frameworks, and then PHP
  2. On the CLI interpreter option, click the more icon (...)
  3. Go to Configuration options, click the folder icon, then the add icon (+)
  4. Set the directive xdebug.client_host and value 127.0.0.1
  5. Click OK

You should see something like the below:

You'll then need to make sure that you're using the right port:

  1. Go to Settings, then Language & Frameworks, then PHP, and select Debug
  2. Check the Debug port in the Xdebug section is set to 9003

You can then open your Xdebug tunnel.

  1. Go to your terminal and select your project folder
  2. Run $ frontastic xdebug
  3. Set a breakpoint in one of your tests and execute it by clicking the debug icon

Did this page help you?