PhpStorm 2024.2 Help

Pest

PhpStorm comes with support for the Pest testing framework. You can run, run with code coverage, or debug Pest tests from the file editor or using PhpStorm's run/debug configurations.

The IDE understands the Pest expectation API, so you can use code completion, find usages, and other coding assistance features when working with Pest assertions and the user-defined test methods.

Install and initialize Pest

To install Pest from within PhpStorm:

  1. Inside composer.json, add the pestphp/pest dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

  3. In the Terminal tool window, execute the ./vendor/bin/pest --init command to initialize Pest in your current PHP project and add the tests folder with example test files and the phpunit.xml configuration file at the root level of your test suite.

Clicking the Settings button next to the package record in the composer.json editor gutter will take you to the corresponding Settings page where you can configure Pest manually.

Gutter icon for pest settings in composer.json

Integrate Pest with a PhpStorm project

If you use a local PHP interpreter, PhpStorm performs initial Pest configuration automatically. In the case of remote PHP interpreters, manual Pest configuration is required.

Have PhpStorm configure Pest automatically

When you install Pest in your project, PhpStorm detects the installed Pest executable and creates a test framework configuration on the Test Frameworks page.

Pest configurations

If the phpunit.xml or phpunit.xml.dist configuration file is detected under the project (or subproject) root automatically or specified explicitly during manual configuration, PhpStorm also creates a respective Pest run/debug configuration.

Pest run/debug configurations

    Configure Pest manually

    1. In the Settings dialog (Ctrl+Alt+S) , go to PHP | Test Frameworks.

      On the Test Frameworks page that opens, click Add icon in the central pane and choose the configuration type from the list:

      ps_settings_php_test_frameworks.png
      • In local configurations, the default project PHP interpreter is used. For more information, refer to Default project CLI interpreters.

      • To use Pest with a remote PHP interpreter, choose one of the configurations in the dialog that opens:

        ps_settings_php_test_frameworks_pest_choose_php_interpreter.png
    2. In the Test Runner area, appoint the configuration XML file to use for launching and executing scenarios.

      By default, Pest looks for a phpunit.xml or phpunit.xml.dist configuration file in the project root folder. You can appoint a custom configuration file.

    Generate a Pest test for a class

    1. In the editor, place the caret at the definition of the PHP class to be tested, press Alt+Enter and select Create New Test from the popup menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.

      To create a test for a certain method, place the caret within the method declaration. The chosen method will be automatically selected from the list of methods in the Create New PHP Test dialog.

    2. In the Create New PHP Test dialog that opens, provide the parameters of the generated test.

      the Create new pest test dialog
      • Test file template: the template based on which PhpStorm will generate the test class. Make sure that Pest is selected from the list.

      • Name: the name of the test class. PhpStorm automatically composes the name from the production class name as .

      • Directory: the folder for the test class file, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its PSR-4 package prefix, or the value specified in the configuration file.

        To specify a different folder, click the Browse button next to the Directory field and choose the relevant folder.

      • Namespace: the namespace the test class will belong to, which is automatically suggested based on the containing directory and namespace of the production class, the configured test sources root and its psr-4 package prefix.

      • Member: the list of production class methods to generate test method stubs for. Select the checkboxes next to the required production class methods. To include inherited methods from parent classes, select the Show inherited methods checkbox.

        PhpStorm will automatically compose the test methods' names as test<production method>. You can customize the code templates used for generating test method stubs on the Code tab of the File and Code Templates settings page.

    After the test is created, you can navigate back to the production class by choosing Navigate | Go to Test Subject. For more information, refer to Navigate between a test and its test subject.

    Run and debug Pest 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 launches the tests. You can later save this configuration for reuse.

    Run or debug Pest tests

    • In the Project tool window, select the file or folder to run your tests from and choose Run '<file or folder>' or Debug '<file or folder>' from the context menu of the selection:

      ps_test_frameworks_pest_launch_tests.png

      PhpStorm generates a default run configuration and starts a run or debug test session with it.

    Run or debug a single test

    You can run or debug a single test for a specific test class or test method from the file editor window.

    • Open the test file in the editor, click in the gutter next to the test and choose Run '<test_name>' or Debug '<test_name>' from the context menu. Alternatively, place the caret at the corresponding line and press Ctrl+Shift+F10.

    Run a single PHPUnit test

    To quickly navigate to a specific test in the test file, use the file structure view. Place the caret anywhere in the open file and press Ctrl+F12 to open the File Structure popup.

    Test file structure popup

    Pressing Alt+7 opens the file structure in the Structure tool window.

    Save autogenerated configuration as permanent

    • After a test session is over, choose Save Configuration from the context menu of the respective run/debug configuration in the run widget.

      Save automatically generated configuration

    Run or debug tests through a previously saved run/debug configuration

    • Choose the required Pest configuration from the list on the toolbar and click the Run button or the Debug button.

    Create a custom run/debug configuration

    1. In the Project tool window, select the file or folder with the tests to run and choose Create run configuration from the context menu. Alternatively, go to Run | Edit Configurations in the main menu, then click Add icon and choose Pest from the list.

    2. In the Pest dialog that opens, specify the scenarios to run, choose the PHP interpreter to use, and customize its behavior by specifying the options and arguments to be passed to the PHP executable.

    Monitor test results

    PhpStorm shows the tests execution results in the Test Runner tab of the Run tool window.

    ps_test_result_pest.png

    The tab is divided into 2 main areas:

    • The left-hand area lets you drill down through all unit tests to see the succeeded and failed ones. You can filter tests, export results, and use the context menu commands to run specific tests or navigate to the source code.

    • The right-hand area displays the raw Pest output.

    Run Pest tests automatically

    You can have PhpStorm 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, a folder, or a composite selection of tests, depending on the test scope specified in this run/debug configuration.

    1. Run the tests.

    2. On the Run toolbar, click Rerun Automatically.

      ps_phpunit_enable-auto-test.png
    3. Optionally, set a time delay for launching the tests upon the changes in the code. To do so, on the Run toolbar, click and select Test Runner Settings | Set AutoTest Delay.

      ps_phpunit_set-auto-test-delay.png
    Last modified: 11 October 2024