Skip to main content

Behat Create your first step definition

Behat provides a way to map your scenario steps 1-to-1 with actual PHP code called step definitions. For example, if you will create new step in your feature file:

Then this is my custom step definition

and execute your code, a corresponding step definition will be generated once you opt for feature context option in console:

/**
  * @Then this is my custom step definition
  */
public function thisIsMyCustomStepDefinition()
{
    throw new PendingException();
}

Step definitions are just normal PHP methods. They are instance methods in a special class called FeatureContext.

The main goal for a step definition is to be executed when Behat sees its matching step in executed scenario. However, just because a method exists within FeatureContext doesn’t mean Behat can find it. Behat needs a way to check that a concrete class method is suitable for a concrete step in a scenario. Behat matches FeatureContext methods to step definitions using pattern matching.

When Behat runs, it compares lines of Gherkin steps from each scenario to the patterns bound to each method in your FeatureContext. If the line of Gherkin satisfies a bound pattern, its corresponding step definition is executed. 

Behat uses php-doc annotations to bind patterns to FeatureContext methods. Notice the comment block starts with /**, and not the usual /*. This is important for Behat to be able to parse such comments as annotations!

If you will copy paste above step definition in FeatureContext class and comment throw new PendingException() line. You will no longer receive FeatureContext has missing step error.

Here, @When is a definition keyword. There are 3 supported keywords in annotations: @Given/@When/@Then. These three definition keywords are actually equivalent, but all three are available so that your step definition remains readable. Below 5 steps need single step definition no matter it starts with @Given/@When or @Then:

Given this is my custom step definition
Then this is my custom step definition
When this is my custom step definition
And this is my custom step definition
But this is my custom step definition

You step can have single or multiple arguments, for example here we are passing 2 arguments:

Then This is my "value1" and "value2"

Corresponding step definition will look like this:

/**
  * @Then This is my :arg1 and :arg2
  */
public function thisIsMyAnd($arg1, $arg2)
{
        throw new PendingException();
}

All token values of the pattern (e.g. :arg1 or :arg2) will be captured and passed to the method argument with the same name ($arg1, $arg2).

Tags
Submitted by tgoswami on December 1, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.