Running PHPUnit tests within PhpStorm

Last updated on
2 April 2017
  1. Copy the /core/phpunit.xml.dist file to /core/phpunit.xml and edit it to include your site's database credentials in the SIMPLETEST_DB variable. (Without this step all tests that require a database will be ignored.)
  2. Go to PhpStorm's settings/preferences. On Mac OS, press cmd + ,. On Linux or Windows, press ctrl + alt + s or go to File > Settings.
  3. Go to Project settings > PHP > PHPUnit (PhpStorm v. 8.x & 9.x) or Languages & Frameworks > PHP > PHPUnit (PhpStorm v. 10.x).
  4. Select Use custom loader. In later versions of PhpStorm, this option is called Use custom autoloader or Use Composer autoloader
  5. For Path to script, enter the path to your Drupal root directory, followed by /autoload.php. You can also set the Default configuration file directly to your Drupal root directory, followed by /core/phpunit.xml. This allows you to debug directly out of the file, without setting up a custom configuration.
  6. Click OK.
  7. Go to Run > Edit Configurations.
  8. Click the green plus sign in the top left corner of the screen and select PHPUnit.
  9. In the Test runner section: for the Test scope option, select Defined in the configuration file, tick the Use alternative configuration file checkbox, and enter the path to your Drupal root directory, followed by /core/phpunit.xml.
  10. Click OK.
  11. Run the test suite by going to Run > Run ..., or press ctrl + R on Mac OS, or shift + F10 on Linux.

If you run your Drupal installation in a Virtual Machine (Vagrant) your configuration has to be a little different, just replace the first steps for these (This functionality is PHPStorm v.8 exclusively):

  1. Configure PHPStorm to know where your vagrant following this configuration: https://confluence.jetbrains.com/display/PhpStorm/Getting+started+with+V...
  2. Add a remote PHP interpreter to your PHPStorm Configuration https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PH...
  3. The next step is to configure the PHPUnit tests to run over SSH on your Vagrant: https://confluence.jetbrains.com/display/PhpStorm/Running+PHPUnit+tests+...
  4. You need to add and extra step to the previous process; On the remote PHPUnit configuration: The Path to script ensure that it is thr path to the mounted folder (local vagrant folder) to your D8 installation plus autoload.php and check the Default configuration file and use your Drupal Vagrant path plus core/phpunit.xml.

Now you can run the tests as shown in step 11, previously

You can limit the tests run to a single group of tests by adding the --group parameter to the Test Runner options. For example, --group Views will run only the Views unit tests.