Acceptance-Test-Driven Development for PHP applications using the Behat framework (using the Moodle project as an example)

The Behat testing framework can be used to write great tests for ATDD

The Behat testing framework advocates the concept of Acceptance-Test Driven Development (ATDD), where human-readable tests are written for highly user-oriented tasks by the developers themselves. This automates the User Acceptance Testing (UAT) process to a certain degree as the tests themselves are not written in highly-technical terminology and follow front-end testing paradigms. Essentially, the idea is that you define a test on how your application should work from the front-end, and then develop that feature from there. Behat works on PHP 5.3 and upwards.

Moodle is a Free and Open Source Learning Management Tool with an active community from all around the world. Moodle is also a good example for extensive use of Behat to test its features. (I should note that Sahana makes use of the Robot framework for ATDD as well)

I thought of covering some of the basics in the Behat framework using examples from how Moodle does it. While Moodle’s implementation might be somewhat different from other projects that use Behat, the basics of each implementation should be reasonably similar.

The following is an example for a typical Behat test, which Behat calls a feature file.

Feature: Login
In order to login
As a moodle user
I need to be able to validate the username and password against moodle

Notice that the feature is written in Gherkin, which is a feature language taken from Cucumber, the ATDD framework for Ruby on Rails. These Gherkin commands are tied in to a PHP function on the Behat framework, for example,

And I press "loginbtn"

ties in to;

* Presses button with specified id|name|title|alt|value.
* @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $button
public function press_button($button) {

in behat_forms.php in the Behat framework. It is also possible to re-use these functions to write very specific test functions for your application. For example, Moodle contains an 'Editing mode' on the Courses module which allows course administrators and teachers to see edit links on various nodes within a course. Moodle's implementation of Behat provides

And I turn editing mode on

which translates to

* Turns editing mode on.
* @Given /^I turn editing mode on$/
public function i_turn_editing_mode_on() {
return new Given('I press "' . get_string('turneditingon') . '"');

which is implemented in the behat_course.php custom class within Moodle's course core module. Note that get_string($string) is a function specific to Moodle's framework.

An interesting feature of Behat is its integration with Mink and Selenium Webdriver to execute the tests on UI itself. Once you run the tests, Selenium will open up a browser window and execute the steps described in each feature file sequentially, while you can watch it being executed.

The Moodle community provides a very comprehensive guide on how to get started with Behat on Moodle, so I will not reproduce that here. Moodle’s implementation of BDD using Behat is very well-thought-out, and a good example for how this testing methodology can be used to automate complex QA tasks in a very effective way.

FOSS Mercenary. Guitarist. Eternal optimist.