Implement BDD in python using behave & allure
So, what's BDD, Behave, Allure? Let's first understand this. Behavior-Driven Development (BDD) is a development approach that enhances collaboration between developers, testers, and non-technical stakeholders by using natural language to define the software’s behavior. It focuses on the what (desired outcomes) instead of the how (implementation). Using Gherkin syntax (Given-When-Then), BDD helps define test scenarios in an easy-to-understand format, making requirements clear for everyone involved. Behave is a Python BDD framework that allows writing tests in Gherkin syntax. It integrates with tools like Selenium to automate browser interactions. Behave lets you define test scenarios in feature files and link them to Python functions (step definitions). It automatically executes these tests to verify application behavior in real-world scenarios. Allure is a popular test reporting framework that provides an interactive, user-friendly interface for visualizing test results. It integrates with various testing frameworks (including Behave) and generates detailed reports that help teams understand the health of their applications. Now, that you understood what each of them means, let's understand how to actually implement BDD in python using behave & allure. Before going through the code, let's see the project structure for clear understanding: .\ │ ├── config\ │ └── configreader.py # File created for some methods to call from defined properties files │ ├── features\ │ ├── login.feature # Sample demo login feature created │ └── loginAgain.feature # Actual usable login feature file used │ ├── screenshots\ # Directory used to store screenshots │ ├── 1.png │ ├── 2.png │ └── 3.png │ ├── steps\ │ ├── test_steps.py # Step def file for the login feature file │ └── utils.py # Additional methods created as a utility file │ ├── xpaths\ │ └── loginPage.properties # Expected xpaths for the login page is described here │ └── config.properties # All the configuration related data is present here This is the low-end structure of it. Folder structure will depend on how big is your project. We've different type of files present: .feature files (gherkin language) .py files (of course) .properties files (where main data is being hold) Following are the recommended (important) libraries to install: behave allure-behave selenium Required Tools: PyCharm Community as an IDLE (Download from here ) First, we need to create a feature file which is being created typically by Business Analyst. Following is the code for sample feature file (login.feature) Feature: Login functionality validation using credentials Scenario Outline: Successful login with provided credentials Given User is on login page When User provides valid login credentials as and Then User will be displayed with a current page title Examples: Valid | username | password | | nitin | kumar | | black | eagle | Examples: Invalid | username | password | | nitin | nitin | Here; we're taking 3 different credentials for testing, 2 are valid & last one is invalid credentials. Now, that we've created the feature file, we need to create a step def file as well to implement them. So, we've test_steps.py as step def files should always starts from 'test_' keyword. First, we need to import all the libraries, methods & necessary data we've created: import time from behave import given, when, then from selenium import webdriver from selenium.webdriver.common.by import By from config.configreader import * from utils import * Now, we need to implement all the @given, @when & @then from feature files. @given('User is on login page') def step_impl(context): context.driver = webdriver.Chrome() context.driver.get(get_property_value('URL')) addScreenshot(context, '1') time.sleep(2) here; 1st & 2nd line is understandable. In 3rd line, we're initializing Chrome driver. In 4th line, we're importing some data 'URL' from properties file & in 5th line, I'm taking screenshot & adding it into our allure report with filename as '1'. Then, I'm taking a 2secs nap. @when('User provides valid login credentials as {username} and {password}') def step_impl(context, username, password): context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHusername')).send_keys(username) context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHpassword')).send_keys(password) addScreenshot(context, '2') context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHloginBtn')).click() Here; we're just providing credentials & clicking on login button. @then('User will be displayed with a current page title') def step_impl(context): time.sleep(2) assert context.driver.title == get_property_value( 'titleHomepage'), 'Wrong title captu

So, what's BDD, Behave, Allure? Let's first understand this.
Behavior-Driven Development (BDD) is a development approach that enhances collaboration between developers, testers, and non-technical stakeholders by using natural language to define the software’s behavior. It focuses on the what (desired outcomes) instead of the how (implementation). Using Gherkin syntax (Given-When-Then), BDD helps define test scenarios in an easy-to-understand format, making requirements clear for everyone involved.
Behave is a Python BDD framework that allows writing tests in Gherkin syntax. It integrates with tools like Selenium to automate browser interactions. Behave lets you define test scenarios in feature files and link them to Python functions (step definitions). It automatically executes these tests to verify application behavior in real-world scenarios.
Allure is a popular test reporting framework that provides an interactive, user-friendly interface for visualizing test results. It integrates with various testing frameworks (including Behave) and generates detailed reports that help teams understand the health of their applications.
Now, that you understood what each of them means, let's understand how to actually implement BDD in python using behave & allure.
Before going through the code, let's see the project structure for clear understanding:
.\
│
├── config\
│ └── configreader.py # File created for some methods to call from defined properties files
│
├── features\
│ ├── login.feature # Sample demo login feature created
│ └── loginAgain.feature # Actual usable login feature file used
│
├── screenshots\ # Directory used to store screenshots
│ ├── 1.png
│ ├── 2.png
│ └── 3.png
│
├── steps\
│ ├── test_steps.py # Step def file for the login feature file
│ └── utils.py # Additional methods created as a utility file
│
├── xpaths\
│ └── loginPage.properties # Expected xpaths for the login page is described here
│
└── config.properties # All the configuration related data is present here
This is the low-end structure of it. Folder structure will depend on how big is your project.
We've different type of files present:
- .feature files (gherkin language)
- .py files (of course)
- .properties files (where main data is being hold)
Following are the recommended (important) libraries to install:
Required Tools:
- PyCharm Community as an IDLE (Download from here )
First, we need to create a feature file which is being created typically by Business Analyst. Following is the code for sample feature file (login.feature)
Feature: Login functionality validation using credentials
Scenario Outline: Successful login with provided credentials
Given User is on login page
When User provides valid login credentials as and
Then User will be displayed with a current page title
Examples: Valid
| username | password |
| nitin | kumar |
| black | eagle |
Examples: Invalid
| username | password |
| nitin | nitin |
Here; we're taking 3 different credentials for testing, 2 are valid & last one is invalid credentials.
Now, that we've created the feature file, we need to create a step def file as well to implement them. So, we've test_steps.py as step def files should always starts from 'test_' keyword.
First, we need to import all the libraries, methods & necessary data we've created:
import time
from behave import given, when, then
from selenium import webdriver
from selenium.webdriver.common.by import By
from config.configreader import *
from utils import *
Now, we need to implement all the @given, @when & @then from feature files.
@given('User is on login page')
def step_impl(context):
context.driver = webdriver.Chrome()
context.driver.get(get_property_value('URL'))
addScreenshot(context, '1')
time.sleep(2)
here; 1st & 2nd line is understandable. In 3rd line, we're initializing Chrome driver. In 4th line, we're importing some data 'URL' from properties file & in 5th line, I'm taking screenshot & adding it into our allure report with filename as '1'. Then, I'm taking a 2secs nap.
@when('User provides valid login credentials as {username} and {password}')
def step_impl(context, username, password):
context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHusername')).send_keys(username)
context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHpassword')).send_keys(password)
addScreenshot(context, '2')
context.driver.find_element(By.XPATH, get_loginPage_xpath_value('XPATHloginBtn')).click()
Here; we're just providing credentials & clicking on login button.
@then('User will be displayed with a current page title')
def step_impl(context):
time.sleep(2)
assert context.driver.title == get_property_value(
'titleHomepage'), 'Wrong title captured or wrong web portal redirected !'
addScreenshot(context, '3')
context.driver.quit()
; Here, we're just checking the page's title & validating it with the provided data in config file.
One file which I want to highlight is utils.py file where a method I've created - addScreenshot().
def addScreenshot(context, filename):
screenshot_path = 'screenshots/' + filename + '.png'
# Capture the screenshot
context.driver.save_screenshot(screenshot_path)
# Attach the screenshot to the Allure report
with open(screenshot_path, 'rb') as file:
allure.attach(file.read(), filename, attachment_type=allure.attachment_type.PNG)
; Here, I've captured the screenshot & added into the allure report which is customized with our reporting part.
Now, every file we've created. Other files you can check in my GitHub Repository.
We need to run the project now. To run & see the report, we need a 2-step command to run.
Go to terminal of our IDLE & run following 2 commands.
behave features/loginAgain.feature -f allure_behave.formatter:AllureFormatter -o loginAgain
; loginAgain.feature is the feature file we're running & loginAgain is the directory where all the reporting files will be generated
and then ...
allure serve loginAgain
;loginAgain is the same directory we've generated above.
The report will be generated & will be displayed in your browser as html file
Keep Testing | Keep Automating | Keep Learning
Feel free to reach out to me via LinkedIn, Instagram or checkout my Portfolio.