Running PHPUnit tests within PhpStorm
- Copy the
/core/phpunit.xmland edit it to include your site's database credentials in the
SIMPLETEST_DBvariable. (Without this step all tests that require a database will be ignored.)
- Go to PhpStorm's settings/preferences. On Mac OS, press cmd + ,. On Linux or Windows, press ctrl + alt + s or go to File > Settings.
- Go to Project settings > PHP > PHPUnit (PhpStorm v. 8.x & 9.x) or Languages & Frameworks > PHP > PHPUnit (PhpStorm v. 10.x).
- Select Use custom loader. In later versions of PhpStorm, this option is called Use custom autoloader or Use Composer autoloader.
- 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.
- Click OK.
- Go to Run > Edit Configurations.
- Click the green plus sign in the top left corner of the screen and select PHPUnit.
- 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
- Click OK.
- 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):
- Configure PHPStorm to know where your vagrant following this configuration: https://confluence.jetbrains.com/display/PhpStorm/Getting+started+with+V...
- Add a remote PHP interpreter to your PHPStorm Configuration https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PH...
- 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+...
- 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.phpand check the Default configuration file and use your Drupal Vagrant path plus
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.