Running Unit Tests in PhpStorm

Running tests in PhpStorm requires following steps to setup the system.

  1. Configuring the PHP interpreter
  2. Configuring PHPUnit
  3. Creating a run configuration

Configuring the PHP interpreter

In the PhpStorm preferences, select the PHP interpreter PhpStorm should use to run tests with.

PhpStorm PHP preference panel

If you can not select a PHP interpreter because the dropdown is empty, you can add a one by clicking on the button with the three dots beside the dropdown.

Then, click the + (add) button in the top right to select the desired PHP binary on your file system.

Adding a PHP interpreter to PhpStorm

Please refer to the PhpStorm documentation for further information on how to configure a local PHP interpreter.

It is also possible to configure a remote PHP interpreter, which is a common setup when working with Vagrant or Docker based development environments.

Please be aware that the details on configuring the PHP interpreter might vary between PhpStorm versions.

Configuring PHPUnit

After configuring the interpreter, the next step is to set up the PHPStorm preferences for how to execute PHPUnit.

PhpStorm PHPUnit preference panel

  1. Click the Use custom autoloader option.
  2. Select the vendor/autoload.php file in your Magento 2 installation.
  3. Optionally select the dev/tests/unit/phpunit.xml.dist file as the Default configuration file. Doing this step makes creating temporary run configurations more convenient.

Please refer to the PhpStorm documentation for further information on enabling PHPUnit in PhpStorm.

Creating a run configuration

The final step is to create a run configuration to execute the desired tests.

There are many ways to create run configurations in PhpStorm. Here we just show one of the option.

All start by creating a new run configuration. To do so, follow these steps:

  1. Select the Run > Edit Configurations action from the top menu.
  2. Click the + symbol on the top right and select PHPUnit.

Depending on what tests should be included in the run configuration, the next steps differ.

Running all tests

  1. Give the run configuration a descriptive name; for example All Unit Tests
  2. Test Scope: select the Defined in the configuration file radio button
  3. Check the Use alternative configuration file checkbox
  4. Select the file dev/tests/unit/phpunit.xml.dist
  5. Click OK.

All Unit Tests run configuration

Running the tests of one module

  1. Give the run configuration a descriptive name; for example, Example_Module Unit Tests
  2. Test Scope: select the Directory option
  3. Select the directory containing the modules unit tests. PHPUnit will run every file ending with Test.php in the selected directory branch.
  4. Click OK.

Module Unit Tests run configuration

Running the tests in a class

  1. Give the run configuration a descriptive name like the name of the test class; for example, FrontControllerPluginTest.
  2. Test Scope: select the Class option.
  3. Class: enter the fully qualified class name (including the PHP namespace).
  4. File: select the file containing the test class.
  5. Click OK.

Test Class run configuration

A more convenient way to create a run configuration for a test class is to open the class in PhpStorm, and then right-clicking into the class and selecting Create ‘TestClassName’ or Run ‘TestClassName’.

Note that the test class has to inherit from \PHPUnit_Framework_TestCase for PhpStorm to display that option.

Executing the run configuration

First select the run configuration to execute in the run configuration drop-down menu above the main editor window, then click the Play icon beside it.

By clicking the Debug icon (of a bug) next to the Play icon, it is possible to step-debug code during test execution, if the xdebug PHP extension is installed.