Behat: A DevOps Based Open Source Framework for Behaviour Driven Development

0
3545

Behat is a test framework for behaviour driven development, written in the PHP programming language. It is free, open source and is hosted on GitHub. It supports developers by providing continuous communication, deliberate discovery and test automation.

The ways to develop, deploy, test and rebuild software applications have changed a lot over the last decade. Traditional software development processes have been enhanced by high-performance DevOps practices.

DevOps is considered as a software development method by some, while others relate it to the tools and technologies for continuous delivery and configuration management. In general, DevOps is an initiative or movement by which the phases of software development, as well as the involvement of professionals, are integrated with real-time collaboration and communication with version control. One of the key goals of using DevOps is to strengthen the link between the software development process (Dev) and the operations of information technology (Ops) so that the complexities and timelines of the overall system development life cycle can be reduced.

There are a number of open source tools and frameworks available for DevOps for the processes of development, deployment, testing, configuration management and redeployment.

Some of the prominent tools and libraries used along with the DevOps process are listed below.

  • Behat: For test automation
  • Watir: A testing tool
  • Supergiant: For container management
  • Ansible: For IT operations including cloud provisioning, deployment and configuration management
  • Nagios: Infrastructure monitoring
  • SaltStack: Event driven orchestration and configuration management
  • Chef: Cloud computing
  • Docker: Portability
  • Git: Source code management
  • Puppet: Software deployment with reliability, audit and agility
  • Hudson: Testing
Figure 1: Key elements of behaviour driven development (BDD)

Behaviour driven development (BDD)

In actual implementations, BDD refers to a development approach rather than a traditional software tool and is the base of test driven development (TDD).

Classical test driven development (TDD) describes the way in which the specific software performs different functions.

BDD describes the detailed view and process by which the end user makes use of a particular software application. In addition, the BDD approach enables a higher level of communication and collaboration among development team members. This approach is more diversified and focused towards the ability of the system to present a detailed view.

In the case of BDD, the story framework is implemented to describe the detailed architecture and flow of the process to be followed.

In the story framework, there are three key components.

  • Role: User (‘As a’)
  • Feature: Requirements (‘I want’)
  • Benefit: Outcome (‘So that’)

In the story framework, the scenarios are associated so that the internal conditions of the test process can be integrated.

Open source frameworks for behaviour driven development and DevOps

Behat: A PHP based framework for BDD
Behat (http://behat.org) is a powerful, multi-featured framework with a behaviour-driven development (BDD) strategy. It is developed in PHP, and supports the syntax structure of traditional sentences in the English language so that the process of test case writing is easy and understandable to all.

Behat is distributed as free and open source with many functionalities and flexibilities, enabling users to customise it for a higher degree of accuracy and performance in the test cases.

Installation of the Behat framework
According to the official documentation, to work with the Behat framework, the minimum version of PHP is 5.3.3.

Installation Method 1 — Using Composer: Using Composer is the official method to install Behat. Composer refers to the package manager for PHP. In addition to the fresh installation of Behat, the new released version can be updated using this approach.

Once Composer is integrated with PHP installation, the following instruction is executed in the project directory:

$ php composer.phar require --dev behat/behat

After successfully running the above mentioned instruction, the version of Behat that’s been installed can be displayed using the following command:

$ /bin/behat -V

Installation Method 2 — Using PHAR: This is another method to install Behat. In this case, download the behat.phar file from https://github.com/Behat/Behat/releases. After downloading, the behat.phar file is moved to the project directory, and then the following command can be used to check the installed version of Behat:

$ php behat.phar -V

Here, the .phar file refers to the PHP archive. It is a package format, so that the bundle of files and libraries can be created and distributed.

Working with Behat and the syntax structure
In the basic format, the scenarios are written in natural language. A few descriptions of the various scenarios are given below.

  • Given some context
  • And more context
  • When some event
  • And second event occurs
  • Then outcome
  • And another outcome
  • But another outcome

There’s no actual difference between Then, And, But or any of the other words that start each line. These keywords are all made available so that your scenarios are natural and readable.
To work with different examples of BDD, the following scenarios can be analysed.

Example 1 — Internet search: If a user is willing to search the tutorials on ‘Blockchain Programming’ on the Internet, then follow the sequence below:

1. The Internet should be connected
2. Open the Web browser
3. Open a search engine
4. Enter the search string or keyword ‘Blockchain Programming’
5. View results
This process can be followed using Behat as shown below.

Feature: Blockchain Programming
In order to see tutorials
As a researcher
I need to be able to get the tutorials and programming source codes of Blockchain Programming

The above example is a very basic scenario of using the Behat framework for BDD programming, in which the feature and its required aspects are specified.

Figure 2: Official Web portal of Behat

Example 2 — Online search and shopping from an e-commerce portal: In this example, the scenario is to search for a suitable smartphone from an e-commerce portal. There are certain conditions and rules that need to be met.

Feature: Smartphone Search and Online Order
In order to buy mobile phone
As a customer
I need to be able to search and order online for the smartphone with certain conditions

The rules are:

  • Tax is 10 per cent
  • Courier delivery charge for the shopping cart under INR 20,000 is INR 100
  • Courier delivery charge for shopping cart over INR 20,000 is INR 200

Scenario: Buying a single smartphone under INR 20,000
Given there is a mobile cover that costs INR 100
When I add the ‘Mobile cover’ to the shopping cart
Then I should have 1 product in the shopping cart
And the overall price of what’s in the shopping cart should be under INR 23,000

Scenario: Buying a single smartphone over INR 20,000
Given there is a ‘Mobile cover’ that costs INR 100
And I add the ‘Power bank’, which costs INR 5000
When I add the ‘Mobile cover’ to the shopping cart
And I add the ‘Power bank’ that costs INR 5000
Then I should have 1 product in the shopping cart
And the overall price of what’s in the shopping cart should be under INR 30,000

Once the instructions are written, these scenarios are executed using the following command inside the project directory:

$ /bin/behat

On running this, the source code will be associated and tested with the test cases, and the test scenarios will be displayed as ‘failed’ or ‘passed’.
For example, if amazon.in is the website mentioned in the source code for analysis and test automation, this website will be scanned with the mentioned conditions of price, shopping cart and other parameters in the scenario, so that the user can get the accurate output. This approach gives the software quality assurance team a detailed view of the outputs that can be generated using different phrases and idioms mentioned in the scenarios of the test project.

Scope for research and development
A number of FOSS test automation libraries and frameworks are available, with their own features and set of testing strategies. These tools can be used for the scenarios discussed in the article, and the results obtained will reveal how well they fare against Behat.

LEAVE A REPLY

Please enter your comment!
Please enter your name here