Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

Behat with Drupal - Tutorial by interns Jared Beckler and Jordan Meier at Cheeky Monkey Media

Behat with Drupal - Tutorial

On our first day as interns at Cheeky Monkey, we (Jared and Jordan) were given the task of exploring the somewhat uncharted waters of using Behat, an open source BDD (Behavior-driven development) testing framework, with Drupal 7.

 

Why BDD Testing?

We all know that testing is important, but why do we bother with “BDD” testing?

Behavior-driven development testing is exactly what it sounds like, testing the behavior of the site. This makes the tests very different than say a unit test.

Unit tests are often reliant on a small piece of code, such as an individual function, so if you change that function, you often have to change the test. With BDD tests, however, you write plain English “Scenarios” inside of specific “Features” or “Stories” to test how you expect the website to react in response to certain user actions. Having these tests available in your back pocket helps you catch bugs in unpredicted areas of your site when you’re implementing new features.

Now that we have the “why?” out of the way, it is time to get cracking on some serious detective work. We also need a sandbox to play around in with these foreign concepts. We set up a very basic Drupal 7 site on Pantheon and cloned it down on our local machines.

 

The Process:

Being relatively new to the world of development, and with Behat being fairly new to the world of Cheeky Monkey, we didn’t have many clues right off the bat. For the first few days of the project, we were on a quest to gather resources and knowledge. First stop? The wise sage, Google. We discovered that there was not a definitive Behat/Drupal tutorial out there, but there are plenty of little breadcrumbs to go off of. The most helpful resources for us were the Drupal Extension to Behat and Mink and the Behat Docs.

The first few days that we spent trying to piece everything together were filled with a constant flux of blind frustration, complete confusion and wonderful epiphanies. Over the course of around two weeks, we were able to put together a small set of features, or tests. Our intention was that they cover some basic Drupal 7 site functionality and can hopefully be implemented on most Drupal 7 projects going forward.

 

Installation

The following steps are what we ironed out to get Behat up and running on Drupal 7 sites locally.

  1. In your local project directory, create a folder called ‘behat’ inside of your sites folder: PROJECT/sites/Behat

  2. In your new Behat folder, create a composer.json file that looks like this:

Screen Shot 2016-07-06 at 10.53.39 AM.png

  1. From your command line, in PROJECT/sites/behat you will want to run $ composer install to get all of those dependencies installed. In order for this step to work, you will need composer installed on your machine.

  2. You will also need to create a behat.yml file that looks something like this, to configure your testing environment:

Screen Shot 2016-07-06 at 3.54.45 PM.png

6. We now need to initialize Behat. To do this, run: $ bin/Behat --init. This creates the features folder where you will write your tests, and your own FeatureContext.php file, where you can define custom steps. To learn more about this, visit the Behat and Drupal Extension documentation that we listed above.

 

Writing Tests

Now to actually writing the tests! This is the easy part. The tests are written using a language called Gherkin, in files with the extension ‘.feature’.

GitHub user mikecrittenden has a list of predefined Drupal behat steps that are available if you like to look at them in a browser.

The quick and easy way to view these steps, in our opinion, is to run $ bin/behat -dl in your terminal from the PROJECT/sites/behat folder.

Here is an example of a small and simple test to get a sense of how the tests are structured:

Screen Shot 2016-07-07 at 10.43.50 AM.png

 

In the above test, the “Feature” declaration is not processed by Behat as it is there for humans to understand what this .feature file is testing. The @api tag before the “Scenario” calls the Drupal API Driver.

  • Given’ is generally used to define some parameters of the environment of your website.
  • When’ is usually the actions taken by the user.
  • Then’ is usually saved for the expected behaviors of the site.

 

Executing Tests

Once the tests are written, you probably want to run them, right?

Luckily, once everything is correctly installed, running Behat tests is a breeze.

You just implemented a new feature onto your website and now you need to run your tests to make sure it didn’t accidently break a behavior.

In your command line, navigate to the PROJECT/sites/Behat folder and run the simple command $ bin/Behat. This tells Behat to find all of the *.feature files and test them against your website. Once it is done running you should be able to see all of your passing tests, and more importantly, any failing scenarios specifying the exact step that failed.

Now let’s say you have your core set of features and you have just written a new one. You don’t need to run all of the tests just to see if the new one works. In your command line, you start as you did before, just adding the path from your project’s Behat folder to that specific .feature file. For example, you made a new test and named it my_example.feature. You would simply run $ bin/Behat features/my_example.feature in your command line.

 

In Conclusion

While this is still a work in progress for us interns, we have learned a lot about Behat and hope that our new found knowledge will be of some help for the fine developers at Cheeky Monkey Media and for anybody else who wishes to cut back on unpredicted bugs!

 

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us

Comments