Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Getting started with BDD and Behat

Tuesday, April 18th, 2017 by Servage

bdd-behatBehat is a popular behavior-driven development (BDD) framework. In Behavior-driven development, developers write the behavior of their application before implementing it with PHP. This way of working helps you design and plan your application instead of jumping right into coding and thinking afterwards if issues arise. Today we will see how BDD can be used with Behat and other tools that work closely together with it.

BDD Workflow with Behat

The workflow of BDD is quite similar to that of TDD. The first task is to write down the behavior of a feature that is about to be implemented. In Behat, this is done using Gherkin, an easy-to-read language that even those who are not programmers can read and write. These files are called feature files, and their file extension is .feature.

Before starting with Gherkin, Behat should be initialized. If you have installed Behat using Composer, you can initialize it with “vendor/bin/behat –init”. This command creates a minimal set of files required for Behat tests to run.

Creating a Feature File

Now we are ready to create our first feature file. After initializing Behat, you should have a features directory in the directory where you initialized Behat. Let’s create a new file there called create_user.feature. Below is an full example of a feature file.

Feature: Creating a user
In order to grant access to more staff members to the application
As an administrator
I need to be able to create a new user account

Scenario: Creating a user account
Given I am logged in as a user “admin”

And I am on the page “/users/create”
When I fill out and submit the user creation form
Then I should see “User created successfully.”

And A user with a username of “testuser” should exist

As we can see in the example above, Gherkin uses keywords such as “Given”, “When” and “Then” to define what should happen during a test. In addition, Behat automatically passes integers and values inside quotes to your PHP files as we will see soon.

Implementing a Test

Now that we have described what the test should do, it’s time to implement it in PHP. Usually, every line in a feature file corresponds to one PHP function where we test something. This can mean quite a lot of functions. Luckily, Behat can create those functions for us automatically. You can let Behat do this for you with the following command: “vendor/bin/behat –append-snippets –snippets-for=FeatureContext”.

The FeatureContext.php file we used in the above command is the default feature file that Behat created when we ran “behat –init”. If you open that file, you should have a function for each line of the feature file. You should also notice that for each integer or string that you defined in the feature file, you have a parameter for a function called $arg1, $arg2 and so on. For instance, the the last line of our feature file passes a string argument of “testuser” to the function as $arg1. To test whether the user actually exists in the application, you can query your database if it has a username that equals $arg1.

After you have built the other steps of the test in a similar manner, you can run your tests using “bin/behat”.

Getting started with BDD and Behat, 5.0 out of 5 based on 3 ratings
Categories: Software & Webapps, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.