What is BDD
BDD allows you to write human readable stories that describe the behavior of your website. These stories are written from an end user’s perspective, and can then be tested against your application to see if they pass or fail. This allow you to define functionality easily and up front, and often make sense in an agile environment, or where you are making constant changes to your application and you want some sort of re-assurance it is working as intended (i.e. regression tests).
Behat and Gherkin
Behat is a tool that make BDD possible. The language that behat stories are written in is called Gherkin. Gherkin is a line-oriented language that uses indentation to define structure. The start of each line is usually a special keyword, these all mean something in Gherkin.
Feature: Some terse yet descriptive text of what is desired In order to realize a named business value As an explicit system actor I want to gain some beneficial outcome which furthers the goal Scenario: Some determinable business situation Given some precondition And some other precondition When some action by the actor And some other action And yet another action Then some testable outcome is achieved And something else we can check happens too Scenario: A different situation ...
A more concrete example, is the UNIX ls command. A stakeholder may say to you:
Feature: ls In order to see the directory structure As a UNIX user I need to be able to list the current directory's contents Scenario: List 2 files in a directory Given I am in a directory "test" And I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should get: """ bar foo """
The Gherkin documentation can be found online.
PHPUnit and behat
Because of their very different approaches to testing, PHPUnit and Behat actually complement each other very well. A growing number of enterprise-level PHP projects are using a combination of both to achieve the best-possible test coverage.
Key things to keep in mind when writing tests
To help write good behat tests, here are some good guidelines to keep in mind.
Focus on business value
When defining features (or stories) ensure that you are focusing on delivering an item of value. If you only have a few tests for your application, ensure the features that give the most business value are created first.
Focus on the user
A critical component of Gherkin style dictates that you speak with language that the target user can reasonably be expected to understand. This user-centric focus means that everyone will be drawn back from their domains of expertise to think about what the target user of the feature needs. There’s a wide variety of users, too. Features intended for a site visitor may differ from those intended for site administrators, editors, or moderators. As the user varies, it’s possible the language used in a scenario will vary as well.
Integration with Drupal
So you have some BDD features written, and now you want to implement them. There is a Drupal module that will help with the creation of nodes, users, vocabularies and taxonomy terms.
There are 3 drivers available in the drupal behat extension, all with different pros and cons:
The most fully featured driver is the Drupal API driver, it has the limitation that it must be run on the same server that the website is run on.
So now you have your Behat tests set up and running on your development environment, the next step is to automate the Behat tests so that they execute automatically on a dedicated testing server after a new commit is pushed. There are a number of ways to do this:
- Github and TravisCI
- Jenkins and use git post-receive hooks (or Acquia cloud hooks)
Drupal 8 and behat
There is a lot of information out in the Drupal community around Behat that I would encourage you to read and watch:
- Konstantin (from Inviqa) at DrupalCon Amsterdam 2014
- Drupal.org did a Drupal 6 to Drupal 7 upgrade, and for that they invested a lot of time into creating a full test suite (called doobie), and now the test suite is used to help spot regressions in the site
- Large Scale Drupal (LSD) created a whitepaper on BDD with Behat and Drupal which is well worth the read
- High Quality PHP by Benjamin Eberlei
- Drupalize me have a podcast on BDD and Drupal
- Behat documentation
If you have any further information that can be added, feel free to let me know in the comments.