Home » Python » Behavioural Driven Development using Python Behave

About Soumyajit Basu

Soumyajit Basu
Soumyajit is a QA/DevOps engineer by profession and a technology enthusiast by passion. He loves communicating about technology and is an author at his own blog platform as well as in Dzone and Web Code Geeks.

Behavioural Driven Development using Python Behave

1. Introduction

Behave is Behavioural Driven Development that uses Python as its back end language. Behave uses tests written in natural language format, backed up by python code.

2. Installation

The installation of Behave can be done in two ways:

  • You can install Behave using pip. All you have to do is just type in pip install behave.
  • The other way you can install Behave is through its source distribution. All you need to do is download the source package of Behave. Once you have downloaded Behave, just unpack the behave source distribution. After that navigate to the directory and type in python setup.py install.

3. Getting started with Behave

To set up Behave for your automation project there are certain aspects that have to be taken into consideration, which includes setting up your browser configurations in an environment.py file. I am using chrome as the default browser to execute the test scenarios. Here is the code block to set up the browser configuration:

Configure chrome driver for Behave in Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
chrome_options = Options()
def before_all(context):
    context.browser = webdriver.Chrome(chrome_options=chrome_options)
def after_all(context):

The configuration mentioned above sets the parameters for the chrome browser so that it is executed within a controlled environment. If you see the code you will find an attribute context which has been used often. context is an object attribute of Behave which holds several information for the tests that are being executed by the Behave runner. The context object may hold information of your browser configuration, the test cases failing on executing a scenario, the current scenario which is getting executed, the tags that are being used for every scenarios etc.

The context variable in all cases is an instance of the behave.runner.Context class.

The before_all method consists of the code block for running the predefined steps before the actual execution for all the scenarios start, for example initiating the loading of a browser in the system.

The after_all method consists of the code block for running the predefined steps after the actual execution of all the scenarios, for example terminate the executing session and closing the browser.

Once you have the configuration of your browser done, next you have to create a directory steps. Inside the steps directory you have to create a file called steps.py. This file would consist of the implementation for the steps that you would define in the Gherkin format. The following code block would give a brief idea:

Setting up the logic for each feature

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *

@when('I visit url "{url}"')
    def step(context, url):
@when('I click on the element with xpath "{xpath}"')
    def step(context, xpath):
    element = context.browser.find_element(By.XPATH,xpath)
@when('I click on the element with css "{css}"')
    def step(context, css):
    element = context.browser.find_element(By.CSS_SELECTOR,css)

Next you have to create a features directory, all the feature files associated to your tests should be kept in this directory. The feature files would have all your test scenarios written in Gherkin format. The file should be saved in the .feature file format. The code block written below describes how to write a feature file.

The Gherkin format to set up your test cases

Feature: Create and Update an ad instance
 Scenario: Hit the URL and validate title of the page
 When I visit url "<some url>"
 Then the title should match "<title of the page>"

 Scenario: Click on login and switch tab
 When I click on the element with xpath "//span[text()='LOGIN']"
 Then I switch to the new tab
 Then the title should match "Automatad - Login"

 Scenario: Enter username, password and validate "admin dashboard"
 When I enter element with name "email" as "<email>"
 When I enter element with name "password" as "<password>"
 When I click on the element with xpath "//button[@type='submit']"
 Then I check the page load status
 Then I should see the element having "CLASS" as "sidebar-header"

4. Executing scenarios based on tags

There might be situations where it would be necessary to execute specific scenarios of a test case. In such cases we have to utilize the concept of tagging in behave. Tagging helps you execute specific scenarios and helps distributing execution of test scenarios. To tag scenarios you just have to mention your tag keyword and append it with @. For example

if I just want to execute all the scenarios for login I would just add a @login tag before each scenario specific to login. For example:

Configuring tags with your test cases

 Scenario: Hit the URL and validate title of the page
 When I visit url "<baseURL>"
 Then the title should match "<title of the page>"

 Scenario: Click on login and switch tab
 When I click on the element with xpath "//span[text()='LOGIN']"
 Then I switch to the new tab
 Then the title should match "<title of the login page>"

So, if I want to execute the scenarios specific to login I can utilize the @login tag to accomplish it.

5. Executing a Behave test

There are several ways you can actually execute behave scenarios.

  • To execute all the behave scenarios just navigate to the features directory and execute the command behave. All the scenarios in the feature file be executed sequentially.
  • If we are out side the features directory we can execute all scenarios by executing the command behave features/.
  • If you want to execute the scenarios based on tags you have to run the command behave features/ --tags=login. It would run all the scenarios specific to login only.
  • You can also use the --capture option like behave --capture to display all standard output on the console. If you use the option --no-capture with behave like behave --no-capture it would not generate the respective standard output on the console then.

For other options that you can use to control the behave runner, you can visit this page.

6. Generating reports in Behave

Reports in Behave can be generated using the Junit library. When junit is enabled, all stdout and stderr will be redirected and dumped to the junit report, regardless of the --capture and --no-capture options. Junit will generate the report in the xml format. But as more and more test cases and scenarios are added this reporting structure would become cumbersome. Further, it would become tough to identify the passed and the failed scenarios and the reason for a test to fail. Allure which is a third party report generating tool, came out with a python binding to generate reports for test results of Behave, which gives a representable format for all the test cases executed.

6.1 Allure Behave

The allure behave can be installed in the following way:

  • I am using mac as my platform, so at first you would have to install allure in your system. To install allure in your system you just need to type brew install allure.
  • Once allure is intalled in your system you need to install the python binding for allure. To install the allure behave package in python you just have to type in pip install –user allure-behave. Now you can easily generate test reports using allure for behave.
  • To generate reports using allure follow these steps:

behave -f allure_behave.formatter:AllureFormatter -o %allure_result_folder% ./features

allure serve %allure_result_folder%

7. Reference

Behave: http://pythonhosted.org/behave/

GitHub: https://bitbucket.org/CodersDen/python-behave

Do you want to know how to develop your skillset to become a Web Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. Building web apps with Node.js

2. HTML5 Programming Cookbook

3. CSS Programming Cookbook

4. AngularJS Programming Cookbook

5. jQuery Programming Cookbook

6. Bootstrap Programming Cookbook


and many more ....



Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook