Behat is a BDD (behavior driven development) framework for PHP. With BDD, you write human-readable stories that describe the behavior of your application. These stories can then be auto-tested against your application. This tutorial describes how to install, configure and use Behat right from the PhpStorm IDE.
Behat support is currently available in PhpStorm 8 EAP.
|Table of Contents|
To make use of Behat, we first have to install it on our system. Read more about Behat installation in official documentation.
Please note that PHP interpreter should have mbstring extension installed (or just compiled with --enable-mbstring option) to execute Behat tests.
Installing Behat via Composer
Use the Composer | Add dependency... context menu on in your project and install behat/behat package:
via Phar package
You can either download behat.phar package from Behat GitHub repository manually, or automatically install it in from Settings | PHP | Behat | Download behat.phar from...
Behat run configurations can be set in the same way as PHPUnit’s. Usual The usual testing workflow (running with Run run action, UI for running tests and displaying results) is supported.
2. Add a new configuration with with button and select Behat
3. Provide the path to the directory which that contains test scenarios (/Users/user/PhpStormProjects/Behat/features in our case). Test scope can be defined as a Directory, File, Scenario or Defined in configuration file. There are many additional options which can be configured in this dialog (such as alternative Behat configuration file, test runner options, command line options, or actions to be performed before launch).
4. If you haven't configured the path to Behat directory or behat.phar file before, PhpStorm will offer you to do so with a the Fix button in the run configuration dialog. Provide the path to Behat and default configuration file (if needed).
Behat configuration can be always changes changed in Settings | PHP | Behat.
Alternatively, you can use Behat via Remote Interpreter, . The configuration process is similar with the only difference that you should have remote PHP interpreter configured.
Setting Behat run configuration via file/folder context action
Apart from standard workflow which involves providing paths to test scenarios manually, you can invoke Depending on your workflow, in order to create run configuration on the fly you can just invoke the context action Create Run Configuration | Folder name (with Behat icon) _, or create and run it straight away with _with Run | Folder name (with Behat icon) for folders in Project Tool Window:
Same The same actions are available in the Run | Run menu (Alt + Shift + F10 / Ctrl - + Alt - + R):
It works in the same way for context action actions inside a *.feature file in the editor (creating run configuration or running all scenarios from file or single scenario from file):
As stated earlier, usual testing workflow (running with Run run action, UI for running tests and displaying results) is supported similar to the one for PHPUnit.
That means that as soon as the run configuration is created (or you are running/creating test scenarios for folder/file from the context menu action), your tests will be run and the results will be displayed in the test runner tab:
From the test runner tab you can navigate to the source code of the test, sort or sort/ filter /etc results of tests, and much more.
Resolve from step in Gherkin file to Context file
Hover Press and hold Ctrl / Cmd hotkey and hover over a step in Gherkin file with Ctrl (Cmd on Mac OS) hotkey pressed and get : the relevant method from the Context file will be highlighted. Clicking Click on appeared the displayed link immediately navigates you to navigate to the declaration. You can also navigate to the method declaration in the Context file with Ctrl+B (Cmd-B on Mac OS) with an editor caret B / Cmd-B when the editor caret is placed at the step in Gherkin file.
As soon as a navigation action is invoked, you are navigated to the very method in Context file (FeatureContext.php in our case):
Find Usages from Context file to usages in Gherkin files
From any Context file, you can find all the usages of this a method in all *.feature files. Just place a the caret at the method name and invoke press Alt-+F7 (or select Find Usages from the context menu action Find Usages). You will see all the usages of the desired method in project files:
Keyword and step completion
All the Any keywords and steps are completed with a the basic code completion action (Ctrl+Space) in *.feature files. All the steps are indexed from the current project and always up-to-date.
Generation for undefined steps
If any of the steps in you your *.feature file are undefined, the PhpStorm will spot that in a moment highlighting step in question: detect this, and will immediately highlight the step as undefined step reference:
Press Alt+Enter (or click on the light bulb) to get a list of relevant quick-fixes, and then select the one named Create Step Definition. You will be immediately offered prompted to choose the step definition file from your project or create a new one.:
In case of choosing already If you choose an existing step definition file, step definition will be immediately created there with a relevant PHPDoc:
In case If you would like to create a new file, this option should be selected from the list on the previous step:
In the Create New Step Definition File dialog will appear providing you an opportunity to set a , enter the file name, file type and file location:
As soon After as you are done with have set these options, a new step definition file will be created:
Your input is welcome
If you have any bugs to report any bugs and/or feature requests to add related to Behat functionality to the, please do so in our Issue Tracker.