Back to Resources


Posted January 6, 2012

Adding Sauce To Behat


Abstract: The key benefits of Behavior-Driven-Development (BDD) practices are communication enhancement and customer satisfaction. You can read more on that by Dan North and Gojko Adzic. Perhaps the biggest practical challenge that stands in the way of reaping those benefits is the burden of provisioning, installation and maintenance of requisite complex and fussy infrastructure. The recent availability of CI servers such as Jenkins & cloud-based testing services such as Sauce Labs carries the potential to remove that barrier. This post discusses and shows how to integrate Behat, an emerging BDD framework for PHP, with Jenkins and Sauce Labs.

What is Behat?

Behat is a BDD framework for PHP. There are some tools available for BDD like Cucumber for Ruby, SpecFlow for .NET and Lettuce for Python. Behat is the first BDD tool for PHP applications. Developers can also use the PHPSpec framework to implement classes within the Behat projects. Behat is written in PHP by Konstantin Kudryashov. With Behat, you can write human readable stories, which are used as tests to run against your application. Behat can be used for API testing, functional testing and data-driven testing. Developers will do API testing and we will carry on with functional testing (web acceptance testing) with Behat.

Functional Testing with Behat and Mink

Behat is used for acceptance testing (any tests) by executing a Gherkin scenario. Developers can implement integrated classes. Testers start thinking of more workflow level and technical level steps (actions), which turns scenarios to features. Once a tester starts to think of Web Acceptance Testing (functional testing) with browser interaction, another tool called "Mink" comes into the picture. Mink is used for browser emulation (functional testing) where browser interactions takes place. As of now, these are the following Selenium drivers available for browser emulation.

  • Selenium provides a bridge for Selenium RC (Selenium 1).

  • Webdriver provides a bridge for Selenium 2. (Facebook webdriver) for PHP. Currently Sauce Lab integration is not available with Webdriver.

Note: The Behat and Sauce Labs integration is currently only available for Selenium 1. Behat In Action: You must first have pear installed in order to proceed with Behat installation. Now we will run these commands from your terminal window:

$ sudo pear channel-discover $ sudo pear channel-discover $ sudo pear install behat/gherkin-beta $ sudo pear install behat/behat-beta

Test your installation by running this command:

$ behat --version Behat version 2.2.0

Now let's install Mink and run the following commands from the terminal window:

$ pear channel-discover $ pear channel-discover $ pear install behat/mink

Mink is ready to use. We have to include "mink/autoload.php" in your "bootstrap.php" file as shown below:

require_once 'mink/autoload.php';

Start Your Project Navigate to the project root directory and initialize Behat by running these commands:

$ cd /path/to/my/project

$ ls application

$ behat --init +d features - place your *.feature files here +d features/bootstrap - place bootstrap scripts and static files here +f features/bootstrap/FeatureContext.php - place your feature related code here

$ ls application features

$cd features

$ ls bootstrap

$cd bootstrap

$ls FeatureContext.php

This will create a "features" directory and "bootstrap/FeatureContext.php" for you. Now we will jump directly to the project created with a feature file. You can use NetBeans with installed Cucumber plugin for Gherkin syntax highlighting. Project structure will look like this: Directory Structure

Behat has already created a "features" directory and "features/bootstrap" directory with "FeatureContext.php" in it.


We can use this create file to define some constants and some third-party libraries that need to be included in class files. It's not mandatory to have PHPUnit and SauceOnDemand extension installed unless you wish to make PHPUnit assertions in behat tests Make sure you have installed correct version of PHPUnit which supports SauceOnDemandTestCase extension. You can follow SauceLabs blog to install it properly. This file should look like this:

Jan 6, 2012
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.