Behat is an open-sourced behavior-driven testing framework for PHP, that makes it easier than ever to write functionality tests with human-readable assertions. It has sort of taken the Drupal world by storm and for good reason. With the ease at which you can install and write tests, it has made it more difficult to come up with excuses to not test your website and code. (Quick introduction to Behat)

What is behavior-driven testing?

Behavior-driven testing is a subset of test driven development, a methodology where the developer writes tests and then code that will make that test succeed. It's a popular form of development that has the benefit of helping to assure that funcitonality works as expected upon delivery. Behavior-driven testing specifies that tests of any unit of software should be specified in terms of the desired behavior, or business requirements. This is highly relevant because so much of the actual testing of websites for quality assurance comes down to repeating the behaviors that activate or drive the various features of the product.

What makes Behat so special?

Behat makes web acceptance testing easier and faster, but to do this it brings together several different tools. First, it uses Mink as a browser emulator abstraction layer. Essentially it is an open source acceptance test framework for web applications, that allows for easier usage of headless or emulated browsers. Secondally, Behat uses a special language called Gherkin for the writing of the tests. The most important aspect of Gherkin is that it is human readable and designed specifically for behavior descriptions.

Whereas in the existing Drupal SimpleTest testing API you often have to write custom classes to form the assertions you are evaluating, classes which are typically heavy in PHP and code, with Behat you write your test assertions using Gherkin's syntax making them comprehendable by even non-programmers.

For example, the following is an actual Behat test:

Scenario: An anonymous user should see necessary components on the homepage

  • Given I am on the homepage
  • And I am not logged in
  • Then I should see text matching "Welcome to our website!"
  • And I should see text matching "Hello world!"
  • And I should get a "200" HTTP response

When Behat runs, using this code, it can simulate an anonymous user viewing the homepage of the website, and checking to see if certain text strings are visible. This can be an easy way to see if certain features or blocks are active. (You can find some more Behat examples here.)

How do you use Behat with Drupal?

On it's own Behat isn't designed specifically for interacting with a Drupal website and testing various Drupal specific features. Drupal roles are an example of something like this, which while not an original concept, the implementation of roles within Drupal is very specific to Drupal itself. To allow Behat to test against these aspects of Drupal, a team has created the "Drupal Extension" for Behat.

Not really a contributed module, but more of an alternative installation of Behat, the Drupal Extension facilitates the use of the Drupal API and/or Drush to test things like being logged in, evaluating roles, and creating content. These functionalities are critical because it is often the case that different components on a Drupal website have limited access or visibility based on a user's role or logged in status. So, if you intend to use Behat for testing a Drupal website I highly recommend starting immediatly with the Drupal Extension. (Additional Drupal Extension information and instructions.)

Next Steps

Once you have Behat installed and have started creating tests and discovering the power of this tool, you'll want to begin thinking about how Behat can improve your existing development and quality assurance processes. With the ease at which you can write and understand the test assertions, it becomes entirely feasible for a non-technical project manager or even the client to get involved with the writing of user stories and tests, or at the very least sign off on the delivered tests. This type of workflow can free up developers from having to spend work time creating tests, but can also keep the client much more involved in the requirements collection phase, helping to ensure greater transparency and also ownership of the final product.

Have you started using Behat with your Drupal project?

Share your tips or questions in the comments section below!