Configuration¶
Behat has a very powerful configuration system based onPHP configuration files andprofiles.
behat.php¶
All configuration happens inside a single configuration file in thePHP or theYAMLformat. By default, Behat loads the configuration from the first file matching:
behat.yamlorbehat.ymlbehat.yaml.distorbehat.yml.distbehat.dist.yamlorbehat.dist.ymlbehat.phpbehat.dist.phpconfig/behat.yamlorconfig/behat.ymlconfig/behat.yaml.distorconfig/behat.yml.distconfig/behat.dist.yamlorconfig/behat.dist.ymlconfig/behat.phpconfig/behat.dist.php
You can also tell Behat where your config file is with the--config option:
$behat--configcustom-config.php
All configuration parameters in that file are defined under a profile name root(default: for example). A profile is just a custom name you can use toquickly switch testing configuration by using the--profile option whenexecuting your feature suite.
The default profile is alwaysdefault. All other profiles inheritparameters from thedefault profile. If you only need one profile, defineall of your parameters under thedefault profile:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;returnnewConfig()->withProfile(newProfile('default')//...);
Overridingdefault params¶
Each profile is an extension of thedefault profile. This means you candefine a new profile that overrides configuration parameters defined in thedefault profile.
Let’s assume we have adefault profile as such:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Filter\TagFilter;useBehat\Config\Profile;useBehat\Config\Suite;$defaultSuite=newSuite('default')->withFilter(newTagFilter('@runthisonlyondefault'));returnnewConfig()->withProfile(newProfile('default')->withSuite($defaultSuite));
Now we want a profile that changes the tag which is to be run in the defaultsuite. We can add the profile and just override:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Filter\TagFilter;useBehat\Config\Profile;useBehat\Config\Suite;$defaultSuite=newSuite('default')->withFilter(newTagFilter('@runthisonlyondefault'));$profile1DefaultSuite=newSuite('default')->withFilter(newTagFilter('@runthisonlyonprofile1'));returnnewConfig()->withProfile(newProfile('default')->withSuite($defaultSuite))->withProfile(newProfile('profile1')->withSuite($profile1DefaultSuite));
Or maybe we want to unset the tag filter for a profile:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Filter\TagFilter;useBehat\Config\Profile;useBehat\Config\Suite;$defaultSuite=newSuite('default')->withFilter(newTagFilter('@runthisonlyondefault'));$profile1DefaultSuite=newSuite('default',['filters'=>null]);returnnewConfig()->withProfile(newProfile('default')->withSuite($defaultSuite))->withProfile(newProfile('profile1')->withSuite($profile1DefaultSuite));
Importing Config¶
Theimport methods can be used to merge multiple configuration files in toone loaded config in Behat, using the following syntax:
<?php// behat.phpuseBehat\Config\Config;returnnewConfig()->import(['config/base.behat.php','config/ci.behat.php',]);
All files from theimport method will be loaded by Behat and merged, inthe listed order, into yourbehat.php config. This is especially usefulwhen you want to tweak configuration slightly between local development andon Continuous Integration environments by using partial configuration files.
This allows configuration files listed in theimport method to overrideconfiguration values for previously listed files.
Global profile configuration¶
You can set some global configuration in your profile configuration:
<?php// behat.phpuseBehat\Config\Config;returnnewConfig()->withProfile(newProfile('default',['testers'=>[// these are the default values'stop_on_failure'=>false,'strict'=>false,],]));
Combining the fact that you can override the default profile, you can change the configuration per profile:
<?php// behat.phpuseBehat\Config\Config;returnnewConfig()->withProfile(newProfile('default',['testers'=>['stop_on_failure'=>true,'strict'=>false,],]))->withProfile(newProfile('ci',['testers'=>['stop_on_failure'=>false,'strict'=>true,],]));
- This way, with the default profile behat will stop on failure and won’t be
strict, but will not stop and will be strict if the CI profile is selected.
You can force--stop-on-failure and--strict via CLI options to overrideconfiguration values.
Environment Variable - BEHAT_PARAMS¶
If you want to set up configurable Behat settings, use theBEHAT_PARAMSenvironment variable:
exportBEHAT_PARAMS='{"extensions" : {"Behat\\MinkExtension" : {"base_url" : "https://www.example.com/"}}}'
You can set any value for any option that is available in abehat.php file.Just provide options inJSON format. Behat will use those options as defaults.You can always override them with the settings in the projectbehat.phpfile (it has higher priority).
Tip
You can convert the PHP configuration to JSON using thetoArray method.
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Filter\TagFilter;useBehat\Config\Profile;$config=newConfig()->withProfile(newProfile('default')->withFilter(newTagFilter('~@wip')));var_dump(json_encode($config->toArray()));{"default":{"gherkin":{"filters":{"tags":"~@wip"}}}}
Tip
In order to specify a parameter in an environment variable, the valuemust not exist in yourbehat.php
Tip
NOTE: In Behat 2.x this variable was inURL format. It has been changedto useJSON format.
Global Filters¶
While it is possible to specify filters as part of suite configuration,sometimes you will want to exclude certain scenarios across the suite,with the option to override the filters at the command line.
This is achieved by specifying the filter in thegherkin configuration:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Filter\TagFilter;useBehat\Config\Profile;returnnewConfig()->withProfile(newProfile('default')->withFilter(newTagFilter('~@wip')));
In this instance, scenarios tagged as@wip will be ignored unless the CLI command is run with a custom filter, e.g.:
vendor/bin/behat--tags=wipCustom Autoloading¶
Sometimes you will need to place yourfeatures folder somewhere other than thedefault location (e.g.app/features). All you need to do is specify the pathyou want to autoload viabehat.php:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;returnnewConfig()->withProfile(newProfile('default',['autoload'=>[''=>'%paths.base%/app/features/bootstrap',],]));
If you wish to namespace your features (for example: to be PSR-1 compliant)you will need to add the namespace to the classes and also tell behat whereto load them. Herecontexts is an array of classes:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Suite;$defaultProfile=newProfile('default',['autoload'=>[''=>'%paths.base%/app/features/bootstrap',],]);$defaultProfile->withSuite(newSuite('default')->withContexts('My\Application\Namespace\Bootstrap\FeatureContext'));returnnewConfig()->withProfile($defaultProfile);
Usingbehat.php to autoload will only allow forPSR-0.You can also usecomposer.json to autoload, which will also allow forPSR-4:
{"autoload-dev":{"psr-4":{"My\\Application\\Namespace\\Bootstrap\\":"app/features/bootstrap"}}}
If you add this to yourcomposer.json file, then you won’t need to specify autoloading inyourbehat.php file:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Suite;$defaultProfile=newProfile('default')->withSuite(newSuite('default')->withContexts('My\Application\Namespace\Bootstrap\FeatureContext'));returnnewConfig()->withProfile($defaultProfile);
Formatters¶
Default formatters can be enabled by specifying them in the profile.
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Formatter\PrettyFormatter;returnnewConfig()->withProfile(newProfile('default')->withFormatter(newPrettyFormatter()));
Extensions¶
Extensions can be configured like this:
<?php// behat.phpuseBehat\Config\Config;useBehat\Config\Profile;useBehat\Config\Formatter\PrettyFormatter;returnnewConfig()->withProfile(newProfile('default')>withExtension(newExtension('Behat\MinkExtension',['base_url'=>'http://www.example.com','selenium2'=>null,])));
Behat