Behat has multiple options for you to split up your FeatureContext into multiple classes. First, you can use old-school php5 inheritance. If the inheritance is not what you want, Behat also supports subcontexts: "Using Subcontexts".
Next, if you want to name your class differently than FeatureContext
, you can redefine that in "Context Configuration" section of your behat.yml
config file.
This way, you could split common definitions and hooks into separate classes and use them in other feature suites as well with either subcontexting or inheritance.
But your question also asks:
I'd like to have one definition file per feature file.
This request is totally wrong. Behat and Scenario BDD is all about describing your application behavior in business terms and creating test dictionary for described behaviors. Keeping that in mind, you logically couldn't have multiple different dictionaries for one feature-set. By writing step definitions, you're telling Behat what that Given I am on "/news"
means. And when you want that step to mean different things from feature to feature - you're doing it wrong.
Behat consists of 2 main and enough separate concepts:
*.feature
files, written in Gherkin language. Those files should be self-descriptive. Means, that they should provide all the information for the reader in order to understand them. Gherkin is not a new programming language for your functional tests, it's just a markdown for your user-stories!
FeatureContext.php
classes, describes how Behat should test your features. It defines application-wide dictionary to be used with whole application feature-suite. This is a programming bridge between your markdown-like user-stories and actual functional tests.
And you shouldn't mess this things up. Single feature suite should have single steps dictionary (definitions). But you could use single dictionary in more than one feature suite thanks to inheritance and subcontexts. And yes, you can split single suite dictionary into multiple php classes ;-)