Submitted by Chris McIntosh on October 15, 2015

These days there is a lot of talk about Agile Development workflow cycles and there is also a lot of talk about Behavioral Driven Development(or BDD for short).  The two can seem to be contradictory, but if the techniques from both are applied properly they can yield big savings in development time and help improve the end result code for a site.  In this article, I am going to go over briefly some of the steps that can be used during Agile when you want your development team to use BDD techniques when delivering a product.

 

Do It Early

Unlike traditional Development methodology which is similar to the old Waterfall Project management methodology, BDD requires that you know and understand what your end deliverable will do before you start writing your code.  This can seem to come at odds with Agile Projects because of how you typically will break down a large seemingly unknown product into much smaller sizes.

Breaking these large projects into smaller pieces actually lends itself well to BDD, because at its core we are talking about how a user is going to behave with the site on the smallest items.  This can be related to stories or Agile tasks for a given project.  This is great news because this also means that when we are first formulating our tasks or stories for a given sprint, we can also build out our BDD tests.  It is a best practice to build out your BDD tests during story grooming or shortly afterward in a sidebar.  This allows you to quickly put into writing what the end goal for any given story is.
 

Selecting a Testing Platform

There are several platforms that can be used in creating tests. Drupal 7 now has SimpleTests as a part of its core code, however, it is a bit hard to digest by those who are not familiar with PHP or Drupal’s API.  There is also PHP’s unit testing framework that could be used for creating tests that meet our behavioral business goals, but again we will struggle with getting our business logic team members to be able to easily read and understand what any of these tests do.

This is where Behat comes in.  Behat is a BDD testing framework.  It uses the Gherkin framework to allow converting plain text English tests into PHP unit tests that can be used to validate a story or task for an Agile project.  It can be written in a way that just about anyone should be able to follow.  Basically, Behat can be thought of as the translator that takes business logic speak and converts it into programmer-speak.  This lets your team start working from the same sheet of paper and will help prevent the issues that come up because a programmer misunderstood the language that a business Logic member used when creating a task or story.
 

Using Behat to write your Tests

Behat tests are broken down into features, with each feature containing any given number of scenarios.  These scenarios are in turn made up of tasks that the testing engine will perform to validate that a feature is successfully built or not.  These tasks are prefaced with basic English, each line starts with “Given,” “When,” “Then,” or “And”.  The rest of the task’s line is pretty much free form and Behat will match it to a PHP function when the test is running.

The important thing when writing a BDD test is to get your test to read as close to the requirements of a given story or task as possible.  Sometimes this will mean sacrificing using an existing task for the greater good of having something that makes sense for those who are in charge of approving business logic.  Later on in the PHP stub function that Behat creates, you will be writing out how to actually prove out that the requirement has actually been met.  This requires thinking through how the end product will look and does not necessarily take into account how the developer may go about getting there.
 

Things to consider for beginners

  • To make things simpler at first, you may want to use one of the many Behat extensions that adds vocabulary to Behat’s dictionary and will allow you to get testing faster.  
  • When implementing Behat with a project and a team that is new to it,  you may want to keep the tests manual; later on, you can automate your Behat tests.  
  • To help with cross-browser testing you can integrate Behat with Selenium2.
  • It may also be helpful to keep a Behat cheatsheet around and read up on best practices for writing a Behat test that makes sense.