Behat is a testing tool, which allows to apply behaviour-driven development (BDD). The basic premise of BDD is to get to know the expectations, needs and goals of the user, in order to create the software which meets all of these demands. BDD was created from a combination of unit tests (to test the correctness of individual components) and acceptance tests (to obtain confirmation of software quality).
BDD is based on the so-called scenarios, that describe the business needs of the software, which, in combination with a tool such as Behat, allows to automate the implementation process. The Behat tests, created by Konstantin Kudryashov, are written in PHP and they support communication between developers and any person using the software during the creation process.
Behat tests can be run on each stage of new code implementation. The first stage is creating feature file for given functionality, in which the scenario is placed. Feature should consist of: easy, clear language, .feature extension, benefits, roles and features of the given functionality.
Feature: Weather Module
In order to get the offer of the product matching my location,
as not logged user,
I need to be able to share my location on my mobile device.
Before starting works on a feature, it is important to talk to a business stakeholder because he may want to introduce rules or conditions for a particular feature. With this knowledge, there are no obstacles to start the stage of creating the scenario.
Each feature can be described by one or more scenarios, which are to explain the way the functionality works in different conditions. Scenarios follow a steady plan, which include scenario description, context, event and result, and they are written in special language, called Gherkin. By default, each scenario consists of three steps: Given, When, Then. For example, a simplified scenario might look like this:
Scenario: Display products from winter collection.
Given the user is in Aspen, when the temperature is -1℃, then the down jacket will be displayed.
This scenario can be further enhanced by the steps And and But. Similarly, the sentence above may look like this:
Given the user is in Aspen, when the temperature is -1℃ and it is snowing, then the down jacket will be displayed and Pom Pom Cup, but not swimsuit.
The above mentioned steps are nothing more than just PHP functions created with a keyword, regular expression, and callback function. The message in the scenario will be treated each time as a step. Steps define what should happen when one of the messages is triggered inside the scenario.
In the next stage, after defining the feature, scenario, and steps, an attempt is made to execute the scenario as a test with the bin/behat command, followed by a message that the test was successful or failed.
The same scenario can be run in different contexts, so different application environments can be tested.
Behat provides a simple, understandable and constructive conversation between a developer and a stakeholder, written in a structured format. Thanks to this, it can be determined what the functionality should look like before implementing it, and to make sure it is of the right quality. Behat also allows to automate checking its behavior under certain conditions. After successful completion of the test, the functionality works in the e-commerce system according to the assumptions used in the scenario.