IntelliJ IDEA 2019.2 Help

Testing with Behat

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

Native support of Behat in IntelliJ IDEA 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

  1. Make sure the PHP interpreter is configured in IntelliJ IDEA 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 later.

  2. Make sure the PHP and Behat plugins are installed and enabled. The plugins are not bundled with IntelliJ IDEA, but they can be installed from the JetBrains plugin repository as described in Managing plugins. Once enabled, the plugins are available at the IDE level, that is, you can use them in all your IntelliJ IDEA projects.

Downloading and installing 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. From the context menu of composer.json, choose Composer | Manage Dependencies. Alternatively choose Tools | Composer | Manage Dependencies from the main menu.

  2. In the Manage Composer Dependencies Dialog that opens, select the behat/behat package from the Available Packages list, possibly using the search field.

    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 field, type the additional command line parameters.

  4. Click Install.

Learn more about installing Behat from Behat Official website.

Integrating Behat with IntelliJ IDEA in a project

If you use a local PHP interpreter, IntelliJ IDEA 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.

IntelliJ IDEA will create the local framework configuration on the Test Frameworks page and the Behat run/debug configuration.

Configure Behat manually

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), expand the Languages and Frameworks node and select Test Frameworks under PHP.

    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, see Default project CLI interpreters for details.

    • 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. IntelliJ IDEA detects the version of Behat and displays it below the field.
  3. 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 checkbox 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 checkbox to specify your own YML configuration file. This file will be later used as default in all Behat run/debug configurations.

      In the field, specify the location of the configuration file to use. Type the path manually or click the Browse button and choose the file in the dialog that opens.

Running and debugging Behat tests

For 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

    IntelliJ IDEA generates a default run configuration and starts a run/debug test session with it.

Save an automatically generated default configuration

  • After a test session is over, choose Save <default_test_configuration_name> from the context menu of the file or folder.

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, choose Run | Edit Configurations from the main menu, then click Add icon and choose Behat from the list.

  2. In the Run/Debug Configuration: Behat dialog that opens, 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.

Monitoring Behat test results

IntelliJ IDEA 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.

Last modified: 17 October 2019