BDD Tutorial with Python Behave | TECHLISTIC

In this tutorial, we'll explain each and every aspect of Behavior Driven Development. This BDD Python tutorial will help you create and design your automation tests and framework.

Table of Content

1. What is BDD?

2. What is Gherkin Language in BDD?

3. What is Behave in Python?

4. Installing and Setup Behave

5. Project Structure for Behave Python (BDD)

6. Feature Files, Step Functions and Other Files in Behave

  • Feature File
  • Step Functions
  • Step Parameters
    • Passing String Param from Feature File
    • Passing Integer Param from Feature File
  • file
  • behave.ini file

7. Run Feature files in Behave

  • Run a single feature file
  • Run multiple feature files
  • Generate JUNIT xml reports with behave
  • Run feature files with Python suite file

Upcoming Tutorials:

8. API Automation Testing with BDD Python (Behave)

9. Selenium Automation with BDD Python (Behave)

1. What is BDD?

BDD stands for Behavior Driven Development. BDD is an agile process of development. It encourages non-technical or business team to collaborate with developers and QA during the development phase of the software. It was originally developed in 2003 by Dan North. This development process is believed to be inspired from the TDD which is Test Driven Development. You can also say that BDD is an extension to TDD.

So, the key idea in BDD is to,
  • Write the test before the development code is written, which should actually fail.
  • Then make that failing test pass after the development code is written.
In BDD, test cases are written in natural language which non-programmers generally business people can understand. Tests are derived from features directly in BDD which certainly give an edge to BDD over other Testing techniques where tests are derived from Technical Specifications. So, it improves the communication and understanding between Developers and Business regarding the required software behavior which is the core goal of BDD.

We are going to explain BDD Automation Testing with Python's Behave module.

2. What is Gherkin Language in BDD?

Tests are written using the Gherkin language in BDD. Gherkin language is basically the normal English language with some basic rules. It has introduced few keywords with which the tests should be written. And these Gherkin language tests are mapped with code written by automation testers which actually gets executed when Gherkin tests are being executed. Gherkin language is used by Behave and many other BDD tools like., lettuce, cucumber etc. Behave in python uses the following eight keywords of Gherkin language:
  1. Given
  2. When
  3. Then
  4. And
  5. Feature
  6. Background
  7. Scenario
  8. Scenario Outline
Riding x
Here is an example of a BDD test written using Gherkin language for the Add Product to cart functionality.
Feature: Add product to cart

    Scenario: Login and click summer dresses
      When I open amazon website
      And I login with username '[email protected]' and password '[email protected]'
      Then I hover on women menu item and click summer dresses
To understand this example better let's understand following aspects:
  • Feature gives a high level description of the software feature.
  • Given is pre-condition.
  • When is some action.
  • Then is the expected outcome.
  • And is used to have additional steps along with Given, When or Then statements. 

3. What is Behave in Python?

Behave is a Python module for implementing BDD approach in Development or Testing code. Behave let's you use Gherkin language to write tests. It let's you create 
  • Feature Files like., Login.feature, Product.feature
  • Step Definition File like.,
  • behave.ini or setup.cfg
These files are the core of BDD. Some pointers about these files.
  • We can create as many feature files as we want or need, say one feature file for one feature under test. 
  • But one step definition and one behave.ini file. 
  • Step definition file is the file where all the code is written and mapped to the steps/lines written in feature file. 
  • And behave.ini is basically a configuration file for the BDD project. 
  • And the environment file is for integrating some hooks to our step implementations like, before_all, before_feature, after_feature etc.
  • We'll learn more about these files as we progresses this tutorial.

4. Installing Behave

Behave can be installed using the pip command or using GitHub repo with file. You just have to open command prompt and execute following commands.

4.1. Using pip command

PYPI distribution URL for Behave -
  • For Windows - pip install behave
  • For Linux - pip3 install behave

4.2. Using GitHub Repo

4.3. Download and Install PyCharm IDE

You can download and install PyCharm IDE from -

PyCharm has two variants, Professional and Community. Professional variant is licensed and Community variant is freeware. You can use the Community variant.

5. Project Structure for Behave (BDD)

Let's understand the structure of a Behave project,
  • Create a new Python project in PyCharm. 
  • And then create a features directory inside it. All the .feature files will be created under this directory.
  • Create a steps directory inside the features directory. Step definition file which is will be created in this steps directory.
So, a simple Behave project will look like this:

|   +--steps/       # -- Steps directory
|   |    +-- *.py   # -- Step implementation or use step-library python files.
|   +-- *.feature   # -- Feature files.

Although a more complex project looks like

+-- features/
|     +-- steps/
|     |    +--
|     |    +--
|     |
|     +--      # -- Environment file with behave hooks, etc.
|     +-- signup.feature
|     +-- login.feature
|     +-- account_details.feature

Project Structure in PyCharm

6. Feature Files and Step Functions in Behave

Now we'll learn how to write feature files and implement steps written in feature file using Gherkin language will be mapped to Python code. It's also called Step Implementation in technical terms.

6.1. How to write a Feature File

As already described in this post that feature file is a collection of steps written in native language, generally English (but Behave supports other languages as well). These steps are written using some pre-defined keywords of Gherkin language, like Given, When, Then, AND etc. In feature file, Feature is the largest entity, then comes the Scenario. A feature can have multiple scenarios in it. A Scenario is basically a test case when it comes to automation testing. Let's understand the different keywords and symbols that can be used in a feature file:
  • Feature: List of scenarios.
    • Background: List of steps run before each of the scenarios
    • Scenario: Business rule through list of steps with arguments.
      • Given: Some precondition step
      • When: Some key actions
      • Then: To observe outcomes or validation
      • And,But: To enumerate more Given,When,Then steps
    • Scenario Outline: List of steps for data-driven as an Examples and <placeholder>
      • Given: Some precondition step with <name>
      • When: Some key actions with <value>
      • Then: To observe outcomes or validation as <status>
      • Examples: Container for s table
        • | name  | value | status  |
          | name1 | 5 | success |
          | name2 | 7 | Fail |
  • | (Data Tables)
  • """ (Doc Strings)
  • @ (Tags/Labels):To group Scenarios
  • < > (placeholder)
  • ## (Comments)
  • "" String

Sample Feature Definition Template

#Author: vb
Feature: Title of your feature
  I want to use this template for my feature file

  Scenario: Title of your scenario
    Given I want to write a step with precondition
    And some other precondition
    When I complete action
    And some other action
    And yet another action
    Then I validate the outcomes
    And check more outcomes

  Scenario Outline: Title of your scenario outline
    Given I want to write a step with <name>
    When I check for the <value> in step
    Then I verify the <status> in step

      | name  | value | status  |
      | name1 |     5 | success |
      | name2 |     7 | Fail    |

Now let's create a feature file named Login.feature in your "features" directory.

Project Path: MyProject/features/Login.feature

#Author: vb
Feature: Login to portal

  # Setup steps for all scenarios of this feature
  Background: Given inputs for this feature
    Given xyz website and it's DB is up

   # Test Case 1
  Scenario: Test Login Functionality
    When I open xyz website
    And I login with username '[email protected]' and password '[email protected]'
    Then I verify that I successfully logged in
    And  I log out

Alright, we have created a feature file, now it's time to implement it's step functions in Python module. 

6.2. Step Functions

Step functions are the implementation of the feature file steps. They are implemented in Python modules present in your "steps" directory. All the Python files (.py) are loaded before executing your features, because .py files are used to search the step implementations.

Let's create a python module inside your project's "steps" directory and name it

Project Path: MyProject/features/steps/

from behave import *

@given("xyz website and it's DB is up")
def step_impl(context):
    raise NotImplementedError(u'STEP: Given xyz website and it\'s DB is up')

@when("I open xyz website")
def step_impl(context):
    raise NotImplementedError(u'STEP: When I open xyz website')

@then("I verify that I successfully logged in")
def step_impl(context):
    raise NotImplementedError(u'STEP: Then I verify that I successfully logged in')

@step("I log out")
def step_impl(context):
    raise NotImplementedError(u'STEP: And  I log out')

Step File Explanation:
  • Importing Behave: You must have noticed that first line in steps file is to import behave module. 
  • Annotations: You can also observe that every step implementation function has an annotation like., @given, @when, @then and @step. These annotations are mapping these functions to the corresponding steps in feature files.
  • Context in Behave: Context is a very important feature in step implementation file. If you want to share your function variables with other functions then you have to declare them with context. Like.,
    • context.iam_token = Token.get_iam_token()
    • If you have declared this iam_token variable in first step_impl function
    • then it would also be available to all the other step_impl functions.

6.3. Step Parameters

You can also use parameters in your steps in feature files. This really a nice feature to have because by using this feature you reduce the number of steps by parameterizing your step. 

For example., if you have written a step for clicking on a link as "I click on Home". And if you have multiple links then you have to create as many steps as links. But if you parameterize this step like "I click on 'link' ", 'link' is a variable here. So, now we'll pass the variable value from steps and in steps file we write our logic to click on the link which is requested by feature file.

6.3.1 Passing String Parameter from Feature file

Let's learn how we can pass a string value parameter from feature file to steps file (.py) from below example.


Scenario: Test menu links
  When I click on 'Home' link
  And I click on 'Dress' link

We have created only one step and parameterized it with link value by writing it in quotes. Now, let's create the step implementation function for this step.

@given("I click on '{link_text}' link")
def step_impl(context, link_text):
    # Call Click link function

So, you can see the syntax to get the string parameter from feature file is to get the value in '{string_value}'. And this varibale then can used further in the step_impl() function.

6.3.2. Passing Integer Parameter from Feature file

Let's take a look at the example feature and step files to understand how to pass integer value from feature file to steps python file.


Scenario: Test Sign-UP form
  Given Age of the employee is '26'

@given("Age of the employee is '{age:d}'")
def step_impl(context, age):
    # Enter age 

6.4. Environment File in Behave

The module can have functions which can run before and after your tests. Let's take a look at those functions and their behavior.

i. before_all(context) and after_all(context) - These run before and after the whole execution.

ii. before_feature(context, feature) and after_feature(context, feature) - These run before and after each feature is executed.

iii. before_scenario(context, scenario), after_scenario(context, scenario) - These run before and after each scenario is run.

iv. before_step(context, step), after_step(context, step) - These run before and after every step. 


import logging

def before_all(context):
     print("Executing before all")

def before_feature(context, feature):
     print("Before feature\n")
     # Create logger
     context.logger = logging.getLogger('automation_tests')

def before_scenario(context, scenario):
    print("User data:", context.config.userdata)

def after_scenario(context, scenario):
    print("scenario status" + scenario.status)

def after_feature(context, feature):
            print("\nAfter Feature")

def after_all(context):
	print("Executing after all")

6.5. behave.ini

It's a configuration file for behave project, a sample behave.ini looks like this,

Sample behave.ini:

color =True

7. Run Your (Tests) Feature files in Behave Project

Behave have many command line arguments, which can be used to run our project code. You can have a list of all available options with behave help command.

behave --help

i. Run a Single Feature file: 

You can run a single feature file with following command,

behave features\Login.feature

ii. Run Multiple Feature files: 

And run all the feature files by giving features directory path to behave command,

behave features\

iii. Generate JUNIT xml Test Report: 

You can also generate the JUNIT test report in xml format by using --junit parameter with behave.

behave features\ --junit

iv. Run Feature files with a Python Suite file: 
You can also create a file in your project and execute your feature files by using that Python file with different behave command line parameters.

Sample file

# -*- coding: utf-8 -*-

import sys
from behave import __main__ as runner_with_options

if __name__ == '__main__':

    # Join Feature Files
    featureFileFolder = '../tests/'

    # Runner Option set to junit
    commonRunnerOptions = ' --no-capture --junit --junit-directory ../test-results/e2e '

    # Complete Suite
    fullRunnerOptions = commonRunnerOptions + featureFileFolder
    var = runner_with_options.main(fullRunnerOptions)

8. API Automation Testing with Behave in Python (BDD)

Coming Soon...

9. Selenium Automation with Behave in Python (BDD)

Coming Soon...

Passionately working as an Automation Developer from 12+ years. Let's connect on LinkedIn

Follow Us

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.


  1. Your website analysis will show you which sites are linking to you and the quality of these sites. newhouse syr edu

  2. It prevents Insurance companies from denying patients coverage for pre-existing conditions, allows people up to age 26 to stay on their parents' Insurance, sets minimum standards for what Insurance must cover and helps lower-income Americans afford health Insurance." But what would really fix the nation is a "single-payer national healthcare program." And good news — the U.S. What Is Nationwide Insurance


Post a Comment

Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

25+ Most Important Selenium WebDriver Commands Tutorial

50+ Most Important Selenium WebDriver Interview Questions and Answers

How to Automate Google Search with Selenium WebDriver

Top 7 Web Development Trends in 2022

How to Find Broken Links on Webpage with Selenium WebDriver