Behat is the official PHP implementation of Cucumber, an open source tool used to drive Behaviour Driven Development (BDD). It encourages collaboration between developers, testers and stakeholders, by promoting writing concrete examples of how software should behave. These documents then become both the specification and the tests for the application.
The BDD software lifecycle
Starting with Discovery, the stakeholder, testers and developers meet to explore what exactly the software should be doing. This can be a fairly informal, interactive discussion, with the ultimate goal being to develop a set of requirements that all are agreed upon.
Following this comes Formalisation, where these requirements are converted into concrete scenarios, which all together become the basis of our new feature file (more on this later).
After this comes the Automation, where upon the acceptance tests are written and run repeatedly for the feature files. Using both Behat and Unit tests and the red-green-refactor approach, this should be done by writing an over-arching acceptance test, then incremental unit tests within, until the acceptance test passes.
Finally the code can be put into production, matching the specification agreed upon by the stakeholder, and continuing to test this specification on subsequent code changes.
The feature file
Behat uses Gherkin syntax, with keywords such as Features, Scenarios and Give/When/Then to create a plain language that is both human readable, and intepretable for automation.
An example can be seen below, of a scenario where a user should be able to process a refund for a purchased item.
Feature: Refund item Scenario: Jeff returns a faulty microwave Given Jeff has bought a microwave for £100 And he has a receipt When he returns the microwave Then Jeff should be refunded £100
The context file
To run the automation, we must give Behat context about what to do when it reads a step from the feature file. This can be an involved process, using regex to parse parameters, and doing some complicated back-end work when necessary. The important thing is to put the complex logic in these context files, and to keep the feature file slim and readable. More information about writing your context files can be found in the official documentation
Other language alternatices