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() chrome_options.add_experimental_option('prefs',{ 'credentials_enable_service':False, 'profile':{ 'password_manager_enabled':False } }) def before_all(context): context.browser = webdriver.Chrome(chrome_options=chrome_options) context.browser.maximize_window() def after_all(context): context.browser.quit()
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): context.browser.get(url) context.browser.implicitly_wait(10) @when('I click on the element with xpath "{xpath}"') def step(context, xpath): element = context.browser.find_element(By.XPATH,xpath) element.click() @when('I click on the element with css "{css}"') def step(context, css): element = context.browser.find_element(By.CSS_SELECTOR,css) element.click()
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
@login Scenario: Hit the URL and validate title of the page When I visit url "<baseURL>" Then the title should match "<title of the page>" @login 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 likebehave --capture
to display all standard output on the console. If you use the option--no-capture
with behave likebehave --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