PhpStorm 2024.3 Help

Behat

With PhpStorm, you can practice behaviour-driven development by running scenarios using the Behat framework. Currently PhpStorm supports integration with Behat 3 and Behat 2 versions.

Native support of Behat in PhpStorm includes:

  • Recognition of and coding assistance for feature scenario files and PHP scenario definition files.

  • Gherkin syntax support in feature files: Feature, Scenario, Given, When, Then, And, and But keywords.

  • Recognition of @given, @when, and @then annotations in definition files.

  • Setting correspondence between scenarios and their definitions through regular expressions in accordance with the PCRE standard for Behat 2.4 and PCRE+ for Behat 3.0. Turnip expressions are also welcome.

Before you start

Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configure local PHP interpreters and Configure remote PHP interpreters. Note that Behat 3 requires PHP 5.5 and later.

Download and install Behat

Before you start, make sure Composer is installed on your machine and initialized in the current project as described in Composer dependency manager.

Download Behat installation package manually

  • Download behat.phar from the Behat Downloads page and save it on your computer:

    • If you need full coding assistance in addition to the ability of running Behat tests, store behat.phar under the root of the project where Behat will be later used.

    • If you only need to run Behat tests and you do not need any coding assistance, you can save behat.phar outside the project.

Download and install Behat with Composer

  1. Inside composer.json, add the behat/behat 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.

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 Behat manually.

Gutter icon for behat settings in composer.json

Learn more about installing Behat from Behat Official website.

Integrate Behat with PhpStorm in a project

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

Configure Behat automatically

  1. Store the behat.yml or behat.yml.dist configuration file under the project root.

  2. Install Behat with Composer.

PhpStorm detects the installed Behat executable (or executables, if the project has Composer-managed subprojects with a specified Behat dependency) and creates a test framework configuration per each installed Behat executable on the Test Frameworks page.

Behat configurations

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

Behat run/debug configurations

Configure Behat 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 Behat with a remote PHP interpreter, choose one of the configurations in the dialog that opens:

      ps_settings_php_test_frameworks_behat_choose_php_interpreter.png
  2. In the Behat Library area, specify the location of the Behat executable file or behat.phar archive.

    Click the Reload button next to the Path to Behat directory or phar file field. PhpStorm detects the version of Behat and displays it below the field.

  3. In the Test Runner area, set the path to the configuration YML file to be used for launching and executing scenarios.

    • Clear the Default configuration file checkbox to have Behat use the behat.yml or behat.yml.dist configuration file from the project root folder. If no such file is found, test execution fails, therefore it may be more reliable to specify the configuration file explicitly.

    • Select the Default configuration file checkbox to specify the path to the YML file to be used as the default configuration file in all Behat run/debug configurations.

Run and debug Behat tests

For more information about writing Behat features, refer to the Behat Documentation.

Run or debug Behat tests

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

    ps_test_frameworks_behat_launch_tests.png

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

Save an automatically generated default configuration

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

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 Behat from the list.

  2. In the Behat 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 Behat test results

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

ps_test_result_behat.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 Behat output.

Run Behat 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