BDD (behaviour-driven development) testing and Behat.

What is BDD?

Behaviour-driven development or BDD is an agile software development process that encourages the colaboration between the customers, the software developers and the QA engineers. We can consider it an evolution from test-driven development (TDD) which is focused on the final software specifications more than in technical details.


What is Gherkin?

Gherkin language defines the structure and a basic syntax for the test cases descriptions, that can be understood by the technical members and the customer. In this way, meanwhile testcases are generated, it is also been creating dynamic documentation about the system behaviour, improving and supporting the documentation.


What is Behat?

Behat is the most popular BDD tool  for PHP programming language. It is a project inspired in Cucumber, that can interpret our features and scenarios using the same format as Gherkin.

Behat is an executable via command line for running the test defined in our features.


What is a Feature?

The element Feature provides the header and the description for the .feature file. It has a title and a text with a high-level description about the goal of the functionality detailed in the file. It contains the list of scenarios that compose the feature, those ones can be group by tags (e.g @regression)

A feature example could be:

behat feature


What is a Scenario?

A scenario is a step-list that begins with some of the following keywords:

  • Given [some context]
  • When [some event]
  • Then [outcome]
  • But or And

Following with the feature created before, we are going to add at the bottom a scenario:

behat scenario

Each feature is defined by one or several scenarios. As we can see in the example, it is possible to add conditions via the keywors And or But.


Executing Behat

Once we had defined a feature and a scenario, we could execute behat. If everything works fine, the console will show us something like this:

execution behat


Writing the steps definitions

As we can see in the previous execution, Behat indicates there are 7 steps undefined. Behat tries to execute the Scenario as a test. However, we hadn’t told Behat what to do with statements as Given I am on “login page”, therefore an error happens. Behat works by matching each statement of a scenario with a list of steps (regular expressions) that we have to write. There, we have to tell Behat what to do when it sees a Given I am on “login page”. We will see how to do it very soon in another post in this blog.

Leave a Comment

¿Necesitas una estimación?

Calcula ahora