How to use Behat for Behaviour-Driven Tests

When developing an application or a set of features, the main aim is to ensure that what has been written is functional. However, that can be a cumbersome task. In the past this might have taken the form of extensive end-user testing, but how do we ease the reliance on others to test our applications, whilst increasing automation and building these processes into development at an early stage?

Taking On Behaviour-Driven Development

Behaviour-Driven Development (BDD) is an evolution of the existing Test-Driven Development (TDD) methodology - an approach to development in which a code is first written to fail, before ensuring that it passes, and further refactoring the code for our application.

So how does BDD differ from TDD? BDD is essentially the shift of thinking about development in "tests" form rather than thinking of them in terms of "behaviour".

Using Behat

Behat is an open source BDD framework for PHP 5.3+. It allows you to write stories that describe the behaviour of your application in a plain English (many other languages are supported as well), human-readable format. Once written, these stories can be used to test against an application.

The Syntax

Behat shares a lot of its fundamentals with Cucumber and the principles are one and the same. Naturally, this means it inherits the same ubiquitous language used by Cucumber, known as Gherkin.

Writing Features in Cucumber

What is known as a "story" in BDD, is specified as a "Feature" in Behat. A feature acts as a shared understanding of the project between stakeholders and those who are creating and maintaining the code.

Behat tests are written in the Gherkin syntax and saved in "Feature" files with an extension of .feature. You should store these files within the Behat directory of your application, but it is recommended to store them within a "features" directory, for ease of referral.

Feature: An application to do whatever In order to do something The users Should be able to visit the web site

Scenario: Logs in to the site Given I am on "/" When I follow "Log In" And I fill in "Username" with "user" And I fill in "Password" with "pass" And I press the "Log in" button Then I should see the "Log out" button And I should see "My Account"

Scenario: Logs out of the site Given I am on "/" When I follow "Log In" And I fill in "Username" with "user" And I fill in "Password" with "pass" And I press the "Log in" button And I follow "Log out" Then I should see "Log in" And I should not see "My Account"

In the above example, Behat uses keywords to defined actions that we expect our application to run.

The Feature

Feature - This is the name of the feature. In order - Describes the benefit of the feature. The users - Describes the role. Should be able to - The feature itself.

The Scenario

  • Scenario - This is the name of the scenario.
  • Given - Describes the initial context.
  • When - Describes the action that the end user performs.
  • And - Adds more events to the action.
  • Then - Describes the expected outcome of the action.

Running Your Tests

Once written in this format, you can run your tests. Running bin/behat from the command line in your Behat directory should run any tests located in the features directory. And just like that, you have a functional suite to automate tests for your application.

Summary

In this article, we have covered the benefits of behaviour driven testing, how it can drive efficiency in the delivery of a project, and introduced you to Behat - an effective and popular tool to plan and implement tests of this development methodology. If you have any further thoughts on Behat or BDD, let us know in the comments below.

We hope you enjoyed reading

Related Articles