Definitions of steps in external files in Behat

Behat, by default, searches for step definitions in a file named FeatureContext (all steps in one file).
With many steps, it is difficult to maintain such a large file.

I would like to have one definition file for each function file.

How to define side definitions in external files?

eg.

 homepage.feature HomepageContext extends FeatureContext 
+8
php testing bdd behat
source share
3 answers

Use class inheritance and separate contexts.

 # /features/contexts/ AbstractContext extends BehatContext {} FeaturenameContext extends AbstractContext {} 

Then in /feature/FeatureContext.php import the context files:

 /** * Initializes context. * Every scenario gets it own context object. * * @param array $parameters context parameters (set up via behat.yml) */ public function __construct(array $parameters) { // import all context classes from context directory, except the abstract one $filesToSkip = array('AbstractContext.php'); $path = dirname(__FILE__) . '/../contexts/'; $it = new RecursiveDirectoryIterator($path); /** @var $file SplFileInfo */ foreach ($it as $file) { if (!$file->isDir()) { $name = $file->getFilename(); if (!in_array($name, $filesToSkip)) { $class = pathinfo($name, PATHINFO_FILENAME); require_once dirname(__FILE__) . '/../context/' . $name; $this->useContext($class, new $class($parameters)); } } } } 
+6
source share

Behat has several options for splitting your FeatureContext into multiple classes. Firstly, you can use php5 inheritance from the old school. If inheritance is not what you want, Behat also supports subcontexts: " Using subcontexts ."

Further, if you want to name your class differently from FeatureContext , you can override this in the Context Configuration section of your behat.yml config.

Thus, you can divide common definitions and bindings into separate classes and use them in other sets of functions, as well as with subcontext or inheritance.

But your question also asks:

I would like to have one definition file for each function file.

This request is absolutely incorrect. Behat and the BDD script is a description of the behavior of your application in business terms and the creation of a test dictionary for the described behavior. With this in mind, you could logically have several different dictionaries for one set of functions. By writing the definitions for the step, you say that it means Given I am on "/news" . And when you want this step to mean different things from function to function, you are doing it wrong.

Behat consists of 2 basic and fairly separate concepts:

  • *.feature files written in Gerkinโ€™s language. These files should be self-describing. So they must provide all the information for the reader to understand them. Gherkin is not a new programming language for your functional tests, it is just a markdown for your user stories!
  • FeatureContext.php , describes how Behat should test your functions. It defines the application dictionary for use with the entire set of application functions. This is a programming modem between your markdown-like user stories and actual function tests.

And you should not ruin it. A separate set of functions should have a dictionary with one step (definitions). But you can use one dictionary in several sets of functions thanks to inheritance and subcontexts. And yes, you can split the dictionary of one set into several php classes ,-)

+24
source share

One solution is horizontal reuse with subcontexts. Use a subcontext for each "function group".

 class FeatureContext extends BehatContext { public function __construct(array $context_parameters) { $this->useContext('math_context', new MathContext()); $this->useContext('bash_context', new BashContext()); } } 
0
source share

All Articles