By Anonymous (not verified) | Tue, 07/19/2016 - 16:08
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.
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.
The following steps are what we ironed out to get Behat up and running on Drupal 7 sites locally.
In your local project directory, create a folder called ‘behat’ inside of your sites folder: PROJECT/sites/Behat
In your new Behat folder, create a composer.json file that looks like this:
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.
You will also need to create a behat.yml file that looks something like this, to configure your testing environment:
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.
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’.
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:
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.
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.
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!