Drupal loves Behat for automated functional testing
Problem Statement : Suppose you build a site, it works great, the client loves it, you launch it, and the client still loves it. Yay! Now life goes on, and six months later, the client comes back to you saying they see a red box when they are logged in, with a message about security updates. You look and see that Drupal core, ctools, rules, views, commerce, date, and a handful of other modules have updates available. Some are security updates, and others are bugfix/feature updates.
So you want to update this code to resolve security issues and improve the functionality of the site. But how can you be sure that these code updates will not hurt or break any of the existing functionality? You could revisit all of your feature work from six months to a year ago and confirm that those features still work as intended. But that can be time consuming and disrupt your other work.
So how do you make updates, whether updating contribution code or doing new custom work, with confidence that you’re not breaking essential functionality and without wasting countless hours doing a bunch of manual testing?
Why Behat? - Behat is a Behaviour Driven Development (BDD) tool which is used to test the behaviour of the application from end user’s point of view. It’s a popular open source tool for automation testing of business cases, using human readable scenarios to map onto the PHP framework.
Behat was inspired by Ruby’s Cucumber project. Both Behat and Cucumber are based on BDD. That’s the only similarity. Cucumber supports many languages (Ruby, Python, Java etc.) but Behat is just supported by PHP.
We use Gherkin, a language quite like English, to write various scenarios/features for testing an application. Behat can also be extended by writing custom PHP functions.
The advantages of using Behat:
Unit testing- This tests that a small piece of code, a function, behaves as expected.
Integration testing- These combine several Unit tests in logical groups, to ensure that they work together properly.
System testing - This tests the system as a whole, and is mainly code oriented, but starts to touch how real people would use the system.
Behavioral testing/Acceptance testing/Customer testing- This involves clickthroughs, user behavior. This is what we are mainly interested in, and what I am talking about today. You will also hear this referred to as BDD or Behavior Driven Development.
Key takeaways for the audience -
Background of what behat is
How does it help when implementing a drupal based application.
Setting up Behat on your machine.
Writing a Behat script for functional testing.
Writing your own custom scripts in Behat.
How to execute Behat functionalities.
Executing Behat using continuous integration. (Jenkins and Probo)
Look out for the wonderful reports generated using Emuse Plugin after Behat tests have finished execution.