Phpstorm Behat

  



Share your Behat project to GitHub using PHPStorm VCS Share your Behat project to GitHub using PHPStorm VCS. Behat is the leading BDD framework for PHP. I use PHPStorm and want to integrate Behat into the IDE as an external tool. Question: How should I set up Behat as an external tool (ie, output filters. I'm trying to use Behat with a remote environment on Vagrant. I have my remote PHP interpreter setup and working, as well as Behat using said interpreter and all is well there. My problem though is that when I run my Behat run configuration, PHPStorm is trying to pass the Windows path to the Behat config, rather than the actual UNIX path on.

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.

Installed

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 section. Press Ctrl+Space to get code completion for the package name and version.

  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.

Click next to the package record in the composer.json editor gutter to jump to the corresponding Settings/Preferences page and configure Behat manually.

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 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, go to PHP | Test Frameworks.

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

    • 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:

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

    Click 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, 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 and choose the file in the dialog that opens.

Run and debug 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:

    PhpStorm 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 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.

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.

Phpstorm

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 Test Runner tab, press the toggle button on the toolbar:

  3. Optionally, click the button and set the time delay for launching the tests upon the changes in the code:

Use this page to integrate PHP-specific testing frameworks with PhpStorm in the current project. With PhpStorm, you can run and debug PHPUnit, Behat, Codeception, and PHPSpec tests.

The page consists of two panes:

  • The central pane shows existing configurations of test frameworks for different interpreters.

  • The contents of the right-hand pane depend on the test framework and the type of the selected interpreter.

Configure a test framework in a project

  1. Choose how you will use the framework. Click and choose the relevant configuration type from the list:

    You can configure any test framework to use with a local or remote PHP interpreter.

  2. For a remote configuration, choose one of the configured PHP interpreters:

  3. In the right-hand pane, choose where to take the test framework from.

    • For Behat, PHPSpec, and Codeception, type the path to the framework executable.

    • For PHPUnit, specify the type of framework installation you are using, the available options are composer autoloaderautoload.php, or phpunit.phar archive, or PEAR.

  4. Check the chosen remote PHP interpreter and path mappings.

  5. Optionally, specify the configuration file. For PHPUnit, you can also specify a bootstrap file to use.

PHPUnit

In this pane, configure installations of PHPUnit to be used with PHP interpreters.

PHPUnit Library

In this area, specify the type of PHPUnit installation. The available options are as follows:

Use Composer autoloaderChoose this option to run PHPUnit installed by the Composer dependency manager. The package is retrieved and loaded by the autoload.php file from the vendor folder. Specify the location of autoload.php in the Path to script field.
Path to phpunit.phar

Choose this option to run PHPUnit from the phar archive.

  • If you already have a phpunit.phar archive in your project, specify its location in the Path to phpunit.phar field. Type the path manually or click and select the file in the dialog that opens.

  • If you have no phar archive on your computer yet, click the Download phpunit.phar... link to have PhpStorm download it automatically.

    In either case, PhpStorm will load the archive before test execution.

CLI Interpreter

The area is shown only for PHPUnit by Remote Interpreter configurations and displays the following:

  • The remote PHP CLI Interpreter to use PHPUnit with. The CLI Interpreter field is read-only. Click to update the chosen interpreter in the Interpreters dialog.

  • The Path Mappings between your local sources and the sources inside the Vagrant instance, or the Docker container, or on the remote host.

  • The Docker container settings that will be used to start the container from an image. These settings may include the volume configuration, the exposed ports, the network, and so on.

In most cases, PhpStorm detects the path mappings and the container settings and fills in all the fields automatically. Alternatively, click next to the field and specify the settings manually. See Configure remote PHP interpreters for details.

Test Runner

In this area, appoint the configuration XML file to use for launching and executing scenarios.
By default, PHPUnit looks for a phpunit.xml configuration file in the project root folder or in the config folder. You can appoint a custom configuration file.

You can also type the path to a bootstrap file to have a PHP script always executed before launching tests. In the field, specify the location of the script. Type the path manually or click and select the desired folder in the dialog that opens.

Default configuration file

Select this checkbox to specify your own XML configuration file. This file will be later used as default in all PHPUnit run/debug configurations. Note that you can also provide an alternative configuration file when editing a PHPUnit run/debug configuration.

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

Clear the checkbox to have PHPUnit use the phpunit.xml 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.

Default bootstrap file

Select this checkbox to have a PHP script always executed before launching tests. In the field, specify the location of the script. Type the path manually or click and select the desired folder in the dialog that opens. Note that you can also provide an alternative bootstrap file when editing a PHPUnit run/debug configuration.

Behat

In this pane, configure installations of the Behat framework available through configured local and remote PHP interpreters.

CLI Interpreter

The area is shown only for Behat by Remote Interpreter configurations and displays the following:

  • The remote PHP CLI Interpreter to use Behat with. The CLI Interpreter field is read-only. Click to update the chosen interpreter in the Interpreters dialog.

  • The Path Mappings between your local sources and the sources inside the Vagrant instance, or the Docker container, or on the remote host.

  • The Docker container settings that will be used to start the container from an image. These settings may include the volume configuration, the exposed ports, the network, and so on.

In most cases, PhpStorm detects the path mappings and the container settings and fills in all the fields automatically. Alternatively, click next to the field and specify the settings manually. See Configure remote PHP interpreters for details.

Behat Library

In this area, specify the Behat installation to use.

Path to Behat executableIn this field, specify the location of the behat.phar archive or the folder with the Behat executable file. Behat does not necessarily have to be installed under the current project root. You can type the path manually or click and choose the relevant location in the dialog that opens.
Behat releasesClick this link to navigate to the Behat repository on github where you can choose the relevant version of behat.phar archive.
Behat versionThis read-only field shows the version of the specified Behat installation. PhpStorm detects the version when you click the Refresh icon . The default value is Not installed.
Phpstorm

Test Runner

In this 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.

Default configuration file

Select this 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 and choose the file in the dialog that opens.

Clear the 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.

Codeception

In this pane, configure installations of the Codeception framework available through configured local and remote PHP interpreters.

CLI Interpreter

The area is shown only for Codeception by Remote Interpreter configurations and displays the following:

  • The remote PHP CLI Interpreter to use Codeception with. The CLI Interpreter field is read-only. Click to update the chosen interpreter in the Interpreters dialog.

  • The Path Mappings between your local sources and the sources inside the Vagrant instance, or the Docker container, or on the remote host.

  • The Docker container settings that will be used to start the container from an image. These settings may include the volume configuration, the exposed ports, the network, and so on.

In most cases, PhpStorm detects the path mappings and the container settings and fills in all the fields automatically. Alternatively, click next to the field and specify the settings manually. See Configure remote PHP interpreters for details.

Codeception Library

In this area, specify the Codeception installation to use.

Path to Codeception executableIn this field, specify the location of the codeception.phar archive or the folder with the Codeception executable file.
Codeception releasesClick this link to navigate to the Codeception repository on github where you can choose the relevant version of codeception.phar archive.
Codeception versionThis read-only field shows the version of the specified Codeception installation. PhpStorm detects the version when you click . The default value is Not installed.

Test Runner

In this area, appoint the configuration InstalledYML file to use for launching and executing scenarios.

By default, Codeception looks for a codeception.yml configuration file in the project root folder. You can appoint a custom configuration file.

Default configuration file

Select this checkbox to specify your own YML configuration file. This file will be later used as default in all Codeception run/debug configurations.

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

Clear the checkbox to have Codeception use the codeception.yml 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.

PHPSpec

In this pane, configure installations of the PHPSpec toolset available through configured local and remote PHP interpreters.

CLI Interpreter

The area is shown only for PHPSpec by Remote Interpreter configurations and displays the following:

  • The remote PHP CLI Interpreter to use PHPSpec with. The CLI Interpreter field is read-only. Click to update the chosen interpreter in the Interpreters dialog.

  • The Path Mappings between your local sources and the sources inside the Vagrant instance, or the Docker container, or on the remote host.

  • The Docker container settings that will be used to start the container from an image. These settings may include the volume configuration, the exposed ports, the network, and so on.

In most cases, PhpStorm detects the path mappings and the container settings and fills in all the fields automatically. Alternatively, click next to the field and specify the settings manually. See Configure remote PHP interpreters for details.

Phpstorm Behat Tests

PHPSpec Library

In this area, specify the PHPSpec installation to use.

Path to PHPSpec executable

In this field, specify the location of phpspec. PHPSpec does not necessarily have to be installed under the current project root.

If no path to PHPSpec is specified for a Local interpreter, PhpStorm does not provide full support of PHPSpec, for example, it does not show suggestions for code completion and does not resolve references.

Prefix ('spec_prefix')

This read-only field shows the namespace prefix for specifications. PhpStorm detects spec_prefix from the configuration file specified in the Default Configuration File field. The default value is spec. See PHPSpec Configuration: PSR-4 and PHPSpec Configuration: Spec and Source Location for details.

The field is shown for Local configurations only.

Phpstorm Bitbucket

Test Runner

In this area, appoint the configuration

Phpstorm Behati Prinsloo

YML file to use for launching and executing specifications.

By default, PHPSpec looks for a phpspec.yml or a phpspec.yml.dist configuration file in the project root folder. You can appoint a custom configuration file.

Phpstorm behati prinsloo

Phpstorm Behat Docker

Default configuration file

Select this checkbox to specify your own YML configuration file. This file will be later used as default in all PHPSpec run/debug configurations.

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

Clear the checkbox to have PHPSpec use the phpspec.yml or phpspec.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.