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 contrib 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?

Detailed overview of session:

Behat is an automated testing system. Its strength is in behavioral testing, so it fits perfectly in our use case.

Behat tests are written in plain English phrases which are then combined into human readable scenarios. This was inspired by Ruby’s Cucumber project and Gherkin syntax. This is probably the most appealing aspect of Behat. Most tests are understandable by anyone, whether you’re a developer, project manager, or business owner.

Behat is the core framework used for running tests. It is capable of testing several types of systems: terminal commands, REST APIs, etc. To enable Behat to test web pages, you need to add Mink and a browser emulator to the mix. Mink functions as the connector between Behat and browser emulators, and provides a consistent testing API.

There are several commonly used browser emulators. Some, like Goutte, are very fast, but don’t support JavaScript. Others, like Selenium and Firefox, are full-featured browsers, but will run more slowly.

So when you hear people talking about Behat, they’re usually talking about all three components: Behat, Mink, and browser emulators.

Types of testing achieved 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.

Even though this all seems like a good thing, it does take some time to write tests, set up a testing environment, and determine what the best tests are. We need to allocate time to do this, and it shouldn’t just be a surprise at the end of the project. Automated testing should be considered in several phases of a web project. When writing custom code, it’s a good practice to write unit tests, and time should be allocated for that. When developing custom features for a site, behavioral tests should be written to accompany them, and again, time should be allocated. It’s good if clients know at the beginning of a project that test writing is part of the development process, and test running is part of deployment.

Things that are measured always get more attention than things that just happen. Clients should have a large say in what is measured and tested. As a result, project managers can gain a better insight into priorities of the client and project. By making behavior tests something that is intentionally done, project stakeholders must clarify and prioritize the most important aspects of the site.

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. These functions are written in FeatureContext.php file which is created inside the bootstrap folder.

Experience Level - Beginners & Intermediate automation testers

Ideal target audience - Testers, Web Developers, BA’s, SM’s

Key takeaways for the audience -

  1. Background of what behat is

  2. How does it help when implementing a drupal based application.

  3. Setting up Behat on your machine.

  4. Writing a Behat script for functional testing.

  5. Writing your own custom scripts in Behat.

  6. How to execute Behat functionalities.

  7. Executing Behat using continuous integration. (Jenkins and Probo)

  8. Look out for the wonderful reports generated using Emuse Plugin after Behat tests have finished execution.