Upgrading to Behat 4.0¶
We want to make upgrading to Behat 4.0 as smooth as possible. While there are some breaking changes, we’ve tried to keep them to a minimum.
If you are an end-user (meaning you use Behat to run tests but haven’t written custom extensions), most of these changes won’t affect you. In many cases, you can use automated tools to handle the upgrade for you. Follow this guide to get started.
Upgrading for users¶
For most projects, these are the main changes:
PHP Configuration: We now use PHP for configuration. YAML files are no longer supported. Behat will look for
behat.phporbehat.dist.phpin your current directory.PHP Attributes: We’ve replaced PHPDoc annotations (like
@Givenor@BeforeScenario) with native PHP Attributes.Cleanup: All previously deprecated features and code have been removed.
New Parser Mode: We now default to a newer parser compatibility mode (GHERKIN_42).
Step-by-step preparation¶
Before you switch to Behat 4.0, we recommend taking these steps:
Update Behat 3: Make sure you are using the latest version of Behat 3.x.
Convert Config: If you use YAML, run
vendor/bin/behat --convert-configto convert it to PHP. Review the results to make sure everything looks correct.Check File Location: If your config file is in a
config/folder, move it to your project root or use the--configflag when running Behat.Update Extensions: Make sure any extensions you use are referenced by their full class name (e.g.
Behat\MinkExtension\ServiceContainer\MinkExtension::classnotBehat\MinkExtension`). The conversion tool usually handles this.Convert Annotations: Use Rector with the
->withAttributeSets(behat: true)rule to automatically change Behat annotations into PHP Attributes.- Parameter Matching: You may see deprecations if your step definition patterns capture a different number
of arguments to the function parameters. These will become an error in 4.0. Fix these by adding the missing parameters to your function, or using non-capturing groups in your regex.
- PHPUnit assertions: We no longer recommend using PHPUnit assertions in your Behat steps, as the
creator of PHPUnit has stated that this is not supported or covered by any backwards compatibility promise. If your steps use PHPUnit assertions, add the behat/phpunit-assertions-extension for improved support in 3.x. You will need this extension in 4.0, as the built-in support has been removed.
Check other Deprecations: Run your tests with
--fail-on-deprecationsand fix any warnings that appear.Test the New Parser: Enable the GHERKIN_42 parser mode and see if your tests still run correctly. If you have issues, you can fix your feature files or use
GherkinCompatibilityMode::LEGACYin your config. This mode will be removed in the future.
Ready to upgrade?¶
Once you’ve completed the steps above, update your composer.json to start using Behat 4.0!
Note
Don’t forget to update your third-party extensions to versions that support Behat 4.0. If you find one that hasn’t been updated yet, consider helping out by submitting a Pull Request! The community’s help makes the transition faster for everyone.