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_32).
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.Check Deprecations: Run your tests with
--fail-on-deprecationsand fix any warnings that appear.Test the New Parser: Enable the GHERKIN_32 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!
Caution
While we are in the alpha phase, we recommend using:
{"require": {"behat/behat": "4.0.0-alpha1@alpha"}}.
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.
Planned changes before the final 4.0.0 release¶
We plan to make two more significant changes before the final 4.0.0 release:
Parameter Matching: We are reviewing how steps behave when the number of function parameters doesn’t match the step definition. This will likely trigger a deprecation or a failure. You can follow the progress in #1691.
PHPUnit Assertions: Support for rendering PHPUnit assertion failures will move to a standalone extension. While tests will still pass or fail, the output will be less detailed without the extension. We no longer recommend using PHPUnit for assertions within Behat steps, as the PHPUnit project has confirmed this is not supported. See #1746 for details.
We may make additional changes based on feedback from the community as more people begin to upgrade.