PhpStorm 2017.2 Help

Testing with PHPUnit

PhpStorm supports unit testing of PHP applications through integration with the PHPUnit tool.

On this page:

How do I integrate PHPUnit with PhpStorm in a project?

Step 1: Choose how to use PHPUnit
Open the Test Frameworks page (File | Settings | Languages and Frameworks | PHP | Test Frameworks for Windows and Linux or PhpStorm | Preferences | Languages and Frameworks | PHP | Test Frameworks for macOS). Click add.png in the central pane and choose the configuration type from the list:

ps_settings_php_test_frameworks.png

Step 2: Choose the PHP interpreter or deployment server to use
To use PHPUnit with a remote PHP interpreter or a web server, choose one of the configurations from the dialog box that opens.

  • Choose a remote PHP interpreter:
    ps_settings_php_test_frameworks_choose_php_interpreter.png
  • Choose a deployment configuration:
    ps_settings_php_test_frameworks_choose_deployment_server.png

Step 3: Choose how to load PHPUnit library
In the right-hand pane, choose one of the methods:

  • Option 1: Download PHPUnit via Composer and add it to the current project.
    Specify the path to the autoload.php file in the vendor folder. See PHPUnit Installation via Composer and Composer for details.
  • Option 2: Run PHPUnit from phpunit.phar
    Specify the path to a previously downloaded phpunit.phar archive. When you click refresh.png, PhpStorm detects and displays the PHPUnit version.
  • Option 3: Install PHPUnit and run it from PEAR
    Pear should be configured as an include path.

    The PHPUnit installation procedure depends on the operating system you use and your system settings. Please, refer to the PHPUnit installation instructions for information on installing and configuring this tool.

Step 4 (optional): Specify the default configuration file
In the Test Runner area, appoint the configuration XML file to use for launching and executing scenarios.
By default, PHPUnit looks for a phpunit.xml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.
You can also type the path to a bootstrap file to have PHP script always executed before launching tests. In the text box, specify the location of the script. Type the path manually or click browseButton.png and select the desired folder in the dialog that opens.

How do I generate a PHPUnit test for a class?

Step 1: Open the Generate PHPUnit Test dialog
In the Project view, select the PHP class to create unit tests for, e.g. MyPHPClass as shown in the image below, and choose New | PHPUnit | PHPUnit Test on the context menu of the selection.

generatePHPUnitTestClass

Step 2: Configure test generation
In the Generate PHPUnit Test dialog, specify the following:

  1. The fully qualified name of the class to be tested, this name will be used to propose the Test Class Name. To use completion, press Ctrl+Space.
  2. The name of the test class. PhpStorm automatically composes the name from the production class name as follows: <production class>Test.php. The test class name is displayed in the Name text box of the Test Class area.
  3. The folder for the test class, by default the folder where the production class is stored. To specify another folder, click /help/img/idea/2017.2/browseButton.png next to the Directory text box and choose the relevant folder.
  4. When the test is ready, navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigating Between Test and Test Subject.

Step 3: Launch test generation
Check, accept, or update the predefined settings and click OK to initiate the test generation.

How do I generate a test for a PHP class defined among others within a PHP file?

Step 1: Open the Generate PHPUnit Test dialog
In the file, select the class to generate the test for and choose Go To | Test on the context menu, then choose Create New Test in the pop-up list.

generatePHPUnitTestSpecificClass

Step 2: Configure test generation
In the Generate PHPUnit Test dialog, proceed as described above: specify the name of the production class, the name of the test class, the name of the test file, and the folder for the test file.
Specify the namespace the test class will belong to. PhpStorm completes the namespace automatically based on the specified directory and displays the generated value in the Namespace text box.
When the test is ready, navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigating Between Test and Test Subject.

How do I generate a PHPUnit test method?

  1. Open the required test class in the editor, position the cursor anywhere inside the class definition, and choose Generate on the context menu, then choose PHPUnit Test Method from the Generate pop-up list.
  2. Set up the test fixture, that is, have PhpStorm generate stubs for the code that emulates the required environment before test start and returns the original environment after the test is over:
    On the context menu, choose Generate | Override method, then choose SetUp or TearDown in the Choose methods to override dialog that opens.
    For more details, see Fixtures on the PHPUnit Official website.

How do I run and debug PHPUnit tests?

You can run and debug single tests as well as tests from entire files and folders. PhpStorm creates a run/debug configuration with the default settings and a launches the tests. You can later save this configuration for further re-use.

Option 1: To run or debug a single test:
Open the test file in the editor, right-click the call of the test and choose Run '<test_name>' or Debug '<test_name>' on the context menu.

Option 2: To run or debug tests from a file:
In the Project view, select the file with the tests to run and choose Run '<file_name>' or Debug '<file_name>' on the context menu.

Option 3: To run or debug tests from a folder:
In the Project view, select the folder with the tests to run and choose Run '<folder_name>' or Debug '<folder_name>' on the context menu.

Option 4: To save an automatically generated default configuration:
After a test session is over, choose Save <default_test_configuration_name> on the context menu of the test, test file, or folder.

Option 5: To run or debug tests through a previously saved run/debug configuration:
Choose the required PHPUnit Test configuration from the list on the tool bar and click /help/img/idea/2017.2/run.png or /help/img/idea/2017.2/debug.png.

How do I monitor test results?

PhpStorm shows the results of test execution in the Test Runner tab of the Run Tool Window. The tab is divided in 2 main areas. In the left-hand area you can drill down through all unit tests to see which ones succeeded and which ones failed. In this area you can also filter tests and export results.
The right-hand area shows us the raw PHPUnit output:

ps_test_result.png
Use the context menu in the left-hand area to run specific tests or navigate to the source code:
ps_test_result_context_menu.png

How do I run PHPUnit tests automatically?

You can configure PhpStorm to re-run tests automatically when the affected code is changed. This option is configured per run/debug configuration and can be applied to a test, a test file, or a folder depending on the test scope specified in this run/debug configuration.

To configure re-running tests automatically:

  1. Run the tests.
  2. In the Test Runner tab, press the ps_icon_phpunit_run_test_automatically.png toggle button on the toolbar:
    ps_phpunit_enable-auto-test.png
  3. Optionally, set the time delay for launching the tests upon the changes in the code:
    ps_phpunit_set-auto-test-delay.png

See Also

Last modified: 19 July 2017