PhpStorm 2017.2 Help

Testing with 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.
  • Support of Gherkin syntax 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 Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters. Note that Behat 3 requires PHP 5.5 and higher.

Where do I get Behat from?

Option 1: Download behat.phar

  1. Download behat.phar from the Behat Downloads page.
  2. Store behat.phar on your computer:
    • To get full coding assistance in addition to simply running Behat scenarios, store behat.phar under the root of the project where Behat will be later used. In this case, PhpStorm will include it in indexing, so references to Behat classes will be successfully resolved.
    • If you only need to run Behat scenarios and you do not need any coding assistance, you can save behat.phar outside the project.

Option 2: Use Composer

  1. On the context menu of composer.json, choose Composer | Manage Dependencies.
  2. In the Manage Composer Dependencies Dialog that opens, select the behat/behat package from the Available Packages list, possibly using the search field. The list shows all the available packages, the packages that are already installed are marked with a tick.

    Choose the relevant version from the Version to install list.

  3. If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters text box, type the additional command line parameters. For example, to have the package added to the require-dev section instead of the default require section, type --dev. For more information about Composer command line options during installation, see https://getcomposer.org/doc/03-cli.md.
  4. Click Install.
Learn more about installing Behat from Behat Official website.

How do I integrate Behat with PhpStorm in a project?

Step 1: Choose how to use Behat

Open the Settings / Preferences dialog by pressing Ctrl+Alt+S, or alternatively choose File | Settings on Windows and Linux or PhpStorm | Preferences on macOS. Expand the Languages and Frameworks node and select Test Frameworks under PHP.

On the Test Frameworks page that opens, 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 to use
To use Behat with a remote PHP interpreter, choose one of the configurations from the dialog box that opens:

ps_settings_php_test_frameworks_behat_choose_php_interpreter.png

Step 3: Specify the Behat library to use
In the Behat Library area, specify the location of the Behat executable file or behat.phar archive. Click refresh next to the Path to Behat directory or phar file text box. PhpStorm detects the version of Behat and displays it below the text box.

Step 4: Specify the Behat configuration file to use In the Test Runner area, appoint the configuration .yml file to use for launching and executing scenarios.
By default, Behat looks for a behat.yml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.

  • Clear the Default configuration file check box to have Behat use the behat.yml configuration file from the project root folder or from the config 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 check box to specify your own .yml configuration file. This file will be later used as default in all Behat run/debug configurations.

    In the text box, specify the location of the configuration file to use. Type the path manually or click browseButton and choose the file in the dialog box that opens.

How do I run and debug Behat tests?

For information about writing Behat features, see http://docs.behat.org/en/latest/user_guide/writing_scenarios.html.

Option 1: To run or debug Behat tests
In the Project tool window, select the feature file to run your tests from and choose Run <feature_name> or Debug <feature_name> on the context menu of the selection:

ps_test_frameworks_behat_launch_tests.png
PhpStorm generates a default run configuration and starts a run/debug test session with it.

Option 2: 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 feature file and choose Save <default_configuration_name> on the context menu.

Option 3: To run or debug tests through a previously saved run/debug configuration
Choose the required Behat configuration from the list on the tool bar and click run or debug.

Option 4: To create a custom run/debug configuration

  1. In the Project view, select the file or folder with the tests to run and choose Create run configuration on the context menu. Alternatively, choose Run | Edit Configurations on the main menu, then click add and choose Behat from the list.
  2. In the Run/Debug Configuration: Behat dialog that opens, specify the the scenarios to run and customize the behavior of the current PHP interpreter by specifying the options and arguments to be passed to the PHP executable file.

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 Behat output:

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

Last modified: 28 November 2017

See Also