Behat, A Quick Introduction

Perhaps the single-most helpful thing I learned about while at the DrupalCampPA was Behat.  Behat is a behavioral driven testing framework that lays out tests in English that are then translated to PHP and other commands.  A simple test might look like the following:

Feature: Listing command
  In order to change the structure of the folder I am currently in
  As a UNIX user
  I need to be able see the currently available files and folders there

  Scenario: Listing two 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 basic structure of a behat test is to define the overall feature in a storytelling context:

  1. Feature to be tested
  2. In order to do X
  3. As a user USERTYPE
  4. I should be able to perform ACTION

The rest of the test consists of steps that a user takes (thus the behavior-driven name), which is written in “regular” language.

But what does it do for me?

As websites become more complex, it is increasingly difficult to test all features manually.  Behat tests provide an automated way of ensuring that features work as intended with each upgrade.  It’s a great safety net to the deployment process.