PhpStorm 2016.1 Help

Using Behat Framework

With PhpStorm, you can practise behaviour-driven development by running scenarios using the BehatBehat 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.

On this page:

Before you start

  1. Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Enabling PHP Support.
  2. Make sure the PHP and Behat plugins are installed and enabled. The plugins are bundled with PhpStorm and activated by default. If the plugins are not activated, enable them on the Plugins page of the Settings / Preferences Dialog as described in Enabling and Disabling Plugins.
  3. Configure the PHP interpreter to use Behat with, as described Configuring Remote PHP Interpreters. Note that Behat 3 requires PHP 5.5 and higher.

Downloading and installing Behat

You can acquire Behat in the following ways:

  • Download the behat.phar archive.
  • Use the Composer dependency manager.

Downloading the behat.phar Archive

  1. Download behat.phar at 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 and you will be able not only to run scenarios but also provided with full coding assistance.
    • If you only need to run Behat scenarios and you do not need any coding assistance, you can save behat.phar outside the project. In this case, PhpStorm will only run Behat scenarios without providing coding assistance.

Installing Behat using the Composer dependency manager

Before you start, make sure Composer is installed on your machine and initialized in the current project, see Using Composer Dependency Manager.

  1. On the main menu, choose Tools | Composer | Add Dependency.
  2. In the Add Composer Dependency 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, however, the packages that have been already installed are marked with a tick.

    Choose the relevant version from the Version to install drop-down 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 to adjust the add dependency procedure. 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.

When you click Create, the create-project command is invoked with the selected package. This results in creating a Composer project whose configuration and structure depends on the selected package, see https://getcomposer.org/doc/03-cli.md#create-project for details. After that a PhpStorm project opens.

Registering the Behat installation in PhpStorm

  1. Open the Settings / Preferences Dialog by choosing File | Settings for Windows and Linux or PhpStorm | Preferences for OS X. Expand the Languages&Frameworks node, and then click Behat under PHP.
  2. On the Behat that opens, specify the location of the Behat executable file or behat.phar archive and the PHP interpreter to use Behat with. This PHP interpreter can be installed either locally on your computer, or in a Vagrant virtual environment, or on a remote host. For information on configuring PHP interpreters, see Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.

    Click the Add toolbar button add in the central panel and do one of the following depending on your workflow:

    • To use Behat with a local PHP interpreter:
      1. Choose Local.
      2. In the Behat Library area, specify the location of the Behat executable file or behat.phar archive.
      3. 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.
    • To use Behat in a Vagrant environment:
      1. Choose By Remote Interpreter. In the Behat by Remote Interpreter dialog box, choose the desired PHP interpreter configuration in a Vagrant environment. When you click OK, PhpStorm returns to the Behat page.

        PhpStorm displays the appointed PHP interpreter configuration in the Interpreter field. The field is read-only, to update the chosen interpreter configuration or create a new one, click the browseButton whereupon PhpStorm brings you to the Interpreters dialog box.

      2. In the Behat Library area, specify the location of the Behat executable file or behat.phar archive in the target Vagrant virtual environment. For example, if you installed Behat through the Composer dependency manager, the location is vendor/bin/behat.
      3. 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.
    • To use Behat on a remote host with SSH access:
      1. Choose By Remote Interpreter. In the Behat by Remote Interpreter dialog box, choose the desired PHP interpreter configuration on a remote host. When you click OK, PhpStorm returns to the Behat page.

        PhpStorm displays the appointed PHP interpreter configuration in the Interpreter field. The field is read-only, to update the chosen interpreter configuration or create a new one, click the browseButton whereupon PhpStorm brings you to the Interpreters dialog box.

      2. In the Behat Library area, specify the location of the Behat executable file or behat.phar archive on the target host.
      3. 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.
  3. In the Test Runner area, appoint the configuration .yaml file to use for launching and executing scenarios.

    By default, Behat looks for a behat.yaml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.

    • To have Behat use the behat.yaml 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. clear the Default configuration file check box.
    • To specify your own .yaml configuration file. Ths 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 the browseButton button and choose the file in the dialog box that opens.

      select the Default configuration file check box.

Creating a Run Configuration

  1. Choose Run | Edit Configuration on the main menu. Alternatively, click Shift+Alt+F10 and select Edit Configuration from the pop-up menu.
  2. In the Edit Configuration dialog box, that opens, click the Add New Configuration toolbar button add.png, and choose Behat on the context menu.
  3. In the Run/Debug Configuration: Behat dialog box, specify 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.

Launching Tests

For information about writing Behat features, see http://docs.behat.org/en/latest/guides/1.gherkin.html. To run or debug your tests, do one of the following:

See Also

Last modified: 12 July 2016