Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

The greatness of Behat for automated testing in Drupal

Parent Feed: 

Visualise yourself getting involved in a social experiment (and you are oblivious of your involvement). You are walking down the street and find cash on the sidewalk. You get drowned in different thoughts as to whether casually pick it up and slip it inside your pocket or just plod away as if you haven’t seen anything or just take it and give it to someone needy. Supposing you have chosen the second option, you, then, realise that it was a social experiment as some guys come towards you appreciating your character.

Illustration showing a human brain with eyes on right and several small white boxes consisting of small icons representing house, car, cup, bowl, emoticon, piechart on left


Sometimes life gives you such opportunities without even giving you some sort of prompt where you are automatically tested for your behaviour. Speaking of an automatic test, there is a different sort of automated test involved in the digital scene but has a lot of significance. Adding test coverage to your website and application can be very fruitful. For instance, you can ensure that requirements are met and avoid regressions from happening. One of the many approaches for testing is Behaviour Driven Development (BDD). As one of the leading open source content management systems, Drupal enables you to imbibe automated testing through tools like Behat in your website.

Diving into Automated testing

Automated testing is a process that involves pre-scripted tests which are run automatically. Their function is to compare actual results with expected results that help in determining if the application works as expected. It can help in executing iterative tasks and regression tests to verify if an application works correctly after the newly implemented alterations.

Automated testing is a process that involves pre-scripted tests which are run automatically.

On the contrary, manually testing involves quality assurance specialists who need to verify that the applications are working properly by following conditions written in test cases. In spite of its primitive nature, it is still very significant in cases like wearables and mobile devices that may need field testing in a physical environment.

Infographics showing different icons inside several yellow-coloured circles with each icons representing clock, spider, tick mark, desktop, curved arrow sign to explain benefits of automated testingSource: Gear Inc

Automated testing saves a lot of time by reducing the time to run repetitive tests which, in turn, saves money. It vastly enhances your test coverage. Lengthy tests that are, most often than not, avoided in manual testing can be run unattended. It also improves precision by performing the same steps precisely whenever they are executed and never misses from recording detailed results. It can simulate tens and thousands of virtual users interacting with a network, software and web applications. Moreover, shared automated tests can be leveraged by developers to detect problems quickly before sending to QA.

Automated testing in Drupal

Drupal 8 core testing framework comes with numerous automated testing options. To ensure the quality and reaction of code on edge cases, testing can be performed in different layers:

  • To test class methods, you can write Unit tests which constitute the process of testing the discrete parts or units of a software project. PHPUnit can be leveraged in this case.
  • For testing module APIs, you can write Kernel tests which are basically unit tests on steroids.
  • You can also adopt functional testing where software testing is performed for detecting the actual performance of an application’s functional requirements. Functional testing is great for accuracy, interoperability, compliance, security and suitability.
  • There is also the possibility of utilising an external framework like Behat with scenarios in gherkin syntax.

Power of Behat

Logo of Behat with the word 'behat' written in black colour and an icon representing number 8


Behat is an open source Behaviour-driven Development (BDD) framework, as stated in the official site of Behat, and helps in supporting the process of delivering software that matters via perpetual communication, deliberate discovery and test automation. BDD is a way of building software through continuous interaction with stakeholders in the form of examples like what help would a particular software be offering to them and you for attaining your goals.

Behat is an open source Behaviour-driven Development framework.

Behat was created for PHP and is a spectacular PHP library to the core with its codebase leveraging a superabundance of Symfony components, strictly adhering to coding standards, and comes out on top in major static analysis tools. Moreover, Behat has superb extensibility as almost all of its functionality could be enhanced or replaced via a magnificent extension system.  

Behat for Drupal

There is a provision for a Drupal extension that offers an integration layer between Behat, Mink Extension and Drupal. For the starters, this Drupal extension can be enabled using Composer followed by the creation of behat.yml file in the projectdir. You also get to add your features files to the features directory of your repository that contains human-readable steps to test something.

By describing the behaviour of a site in plain but stylised language followed by the process of turning that description into an automated test, Behat and Mink come in very handy. These functional tests enable the site builders to ensure that the added value that is created while building a Drupal website keeps on behaving as per the expectations even after alterations are done to the website like security updates, changes to custom code among others.

This Drupal extension helps in setting up test data using Drush or the Drupal API in addition to defining theme regions and test data that appear within them. It also assists in clearing cache, logging out and other such significant steps. Moreover, it is an astronomical solution for detecting and discovering steps offered by contributed modules and themes.

Case study

Global Canopy Programme wanted to retrieve the news syndicated from a plethora of public sources, govern it through an internal application followed by resyndicating it to numerous public-facing sites. For this, it leveraged the power of Drupal with the help of a digital agency to build this application called Forest Desk. They followed the paradigm of BDD that was beneficial for each piece of functionality and could be embedded in the Scrum process.

Behat was meritorious for testing whether the functionality has been built correctly. It turned out to be an essential tool for testing comprehensible action on a website like clicking links or hitting submit buttons. Behat’s knowledge could further be extended using custom PHP code thereby leveraging it to understand RSS which is the format for syndicating content on the web.

Behat’s knowledge could further be extended using custom PHP code thereby leveraging it to understand RSS which is the format for syndicating content on the web.

The RSS standard was utilised for syndicating data and Drupal’s Feeds module was very beneficial in consuming this standard from remote websites and modules like Views helped in repackaging it for consumption by other sites. Furthermore, the Adminimal theme offered an immaculate administrative design.

For developing each feature, once its scenario was specified in Gherkin syntax, Features module was leveraged. This helped in altering the configuration and save the changes and return to the previous state of configuration in case any BDD test no longer works.

Conclusion

Behat is a great tool for enabling superfast and efficacious automated testing in Drupal development.

Offering stupendous digital experience with our suite of services to our partners has been our supreme goal.

Contact us at [email protected] to get the best out of automated testing through this wonderful tool - Behat.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web