Testing is the process of running through the expected use of a site feature with the intent of finding bugs. To automate this process we use a tool called Behat which allows us to create tests in two layers.
The first layer is a readable description of what we are testing, in Behat this is called a Feature. A feature can contain multiple instances of how it works, these are called Scenarios.
The second layer is the implementation where we associate PHP code to the steps defined in a scenario to automate each specific step. If you'd like to find out more about Behat and testing have a look at our benefits of Behat article.
Now lets have a look at how testing your Drupal site has never been easier thanks to Behat 3 and the Behat Drupal extension....
The hardest job is installing Behat and configuring exactly which extensions and drivers you need in place.
Fortunately for us the folks who built the Behat Drupal extension have provided some great documentation, so before I get on to talking about the benefits and limitations of the Behat extension, here's an article that takes you through the steps of setting up Behat with the Drupal extension on your local machine.
Behat Drupal Limitations
So the major limitation that we came up against is that to really make use of all the available functions the Behat Drupal extension provides, you need to run tests from the same server that the site being tested is running on.
Now, dependent on your testing process this may not actually be a limitation.
For us it was, we had multiple environments setup online that we wanted any of our developers to be able to run tests against without requiring them to go through the steps of setting up the site, getting the latest tests and then running them.
To simplify the testing process we used Jenkins to automate [spinning up a virtual server with everything required to run Behat and a copy of the site being tested]. Jenkins is an Open Source automation server written in Java that can automate many different manual processes and it's a great fit tasks like this which require lots of repetative technical steps.
Automating the testing process with Jenkins means that developers only need to click a button and wait until they receive a message once tests had completed with the results.
Behat Drupal Benefits
The main reason for running tests from the same server that the site being tested is that it makes Drush commands available to the tests.
To quote the Drush docs :
"Drush is a command line shell and Unix scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes sql queries and DB migrations, and misc utilities like run cron or clear cache."
You will need to point the driver to your local site directory in your behat.yml configuration file to utilise the available commands. As well as using available functions provided by the Drupal extension you can also create custom functions which can access Drush as well.
One feature we really appreciated is that when tests use the Drupal extension to log in as a specific role, it uses Drush to spin up a new user with the role you specified, does all the following steps in your scenario, and then deletes that user and any content that was created by it before finishing the test. The reason we like this so much is that it means no tests will fail due to it previously running on the same site, which make the tests a lot more reliable and predictable.
So to summarise the benefit is access to Drush features like:
- Clearing Drupal caches
- Running cron
- Creating/deleting users
- Changing a users role
- Creating, editing and deleting content
I could list many more useful features that Drush has, but that will be the topic of another article.
Ready for the bigger picture?
For more in-depth guidance on Behat and BDD come to one of our Quality Assurance training courses.