Power Your Software Testing
with AI and Cloud
Supercharge QA with AI for Faster & Smarter Software Testing
Learn how to use Python Behave for BDD testing. Write human-readable tests, run them efficiently, and follow best practices for seamless collaboration.
Published on: October 18, 2025
Behavior-Driven Development (BDD) in Python is made simple and effective with Python Behave. This framework helps developers create automated tests that reflect real-world user behavior, ensuring that only relevant features are built and verified.
By combining structured test automation with human-readable scenarios, Python Behave helps teams maintain high-quality software while improving collaboration between technical and non-technical stakeholders.
What Is Python Behave?
Python Behave enables writing automated tests in a clear, structured format that both technical and non-technical team members can follow. It translates behavior specifications into executable Python code, helping ensure that software behaves as intended. By separating feature descriptions from implementation, Behave promotes collaboration between developers, testers, and stakeholders while improving test readability and maintainability.
How to Run Python Behave BDD Tests?
Python Behave lets you execute Behavior-Driven Development tests by translating human-readable scenarios into automated Python scripts. Running these tests efficiently requires setting up a proper environment, organizing feature files, and connecting step definitions to their corresponding scenarios.
What Are the Common Troubleshooting Techniques for Python Behave?
Python Behave tests can fail due to environment issues, directory misconfigurations, or mismatched step definitions. Understanding common pitfalls and debugging effectively ensures stable BDD test execution.
What’s the Best Way to Manage Reusable Step Definitions?
For large projects, managing step definitions efficiently is essential. The best practice is to modularize your step files, group related steps into separate Python modules (for example, login_steps.py, api_steps.py, ui_steps.py). Behave automatically discovers these modules if they’re inside the steps directory. Following the same structure recommended in Cucumber best practices, you should also avoid duplicating steps and use descriptive function names to improve readability and maintainability.
Python Behave is a BDD framework that focuses on bridging the gap between business requirements and code. It uses plain English to define scenarios, making it easy for everyone to understand an application's expected behavior.
Scenarios written in the Gherkin language are then linked to Python step definitions, which implement the actual logic to execute and validate those behaviors.
Python Behave relies on several essential components to enable Behavior-Driven Development (BDD) testing in Python. Understanding these elements helps in organizing and executing tests efficiently:
Note: Run your Python Behave tests at scale across 3000+ browsers and OS combinations. Try LambdaTest Now!
For Python projects, the Behave framework streamlines Behavior Driven Development test execution and promotes better collaboration between developers, testers, and non-technical team members.
Prerequisites:
Ensure that the latest version of Python is installed on your local machine, along with the Python package manager, pip.
Use the commands below to verify both installations:
python --version
pip --version
After verifying the installation of Python and the standard package manager on your local machine, you must create a Python virtual environment and install the required project dependencies.
Create a Python Virtual Environment:
Follow the steps below to set up your project environment:
mkdir behavior_driven_development_python_behave
cd behavior_driven_development_python_behave
python -m venv env
env\Scripts\activate
pip install behave behavex selenium requests
With the virtual environment ready and dependencies installed, the next step is to organize your project into the standard Behave directory structure.
Project Structure
A Python Behave project requires a specific structure to organize tests:
behavior_driven_development_python_behave/
└── features/
├── steps/
├── *.feature
└── environment.py
This ensures that Behave can discover your feature files and execute the corresponding step definitions correctly.
Behave feature files are written using the Gherkin language, which is specifically used in BDD to describe the behavior of an application or software feature in plain English.
Gherkin has a standard syntax and keywords that provide a structured pattern for describing scenarios. Scenario steps are described using Given-When-Then, while And and But are sometimes used to provide extra details where necessary.
In every *.feature file, a software feature is described, and one or more scenarios give concrete examples of how the feature should behave. Each scenario essentially represents a Gherkin test case that can be executed to validate the behavior of the application.
Some Gherkin keywords:
Using Gherkin in this way ensures clear Gherkin testing, where each test case is readable in plain English, understandable by both technical and non-technical stakeholders.
API Endpoint Response Implementation:
In the Features directory, you will need to create an api_status.feature file that will contain code for BDD testing of an API response. The Gherkin language is used in Python Behave to write scenarios that describe specific behaviors of the software.
Python Behave provides the same approach as Cucumber testing in other languages, keeping scenarios human-readable and easy to understand. While Gherkin describes scenarios, the step definitions are the code implementation of those scenarios.
Feature: Public API - Post Retrieval
@Chrome
Scenario Outline:
Given the API endpoint is "https://jsonplaceholder.typicode.com/<endpoint>"
When I make a GET request
Then the response status should be "<status_code>"
And the response should contain the field "<field>"
Examples:
| endpoint | status_code | field |
| posts/1 | 200 | userId |
| posts/2 | 200 | title |
| posts/100 | 200 | body |
| posts/101 | 404 | error |
Code Walkthrough:
After creating *.feature files, you need to create step definitions in Python code. The step definitions are Python functions that are mapped to the Scenarios written in Gherkin syntax. These functions define what each step outlined in the Given-When-Then format should do.
The Python files containing Python code definitions for each step are contained in the Steps repository. A decorator with the keyword Given, When, or Then is attached to its function.
API Endpoint Response Python Step Implementation:
After creating a feature file for the API Endpoint Response, a steps file will have a Python definition for the feature. To define the steps to test the behavior of the API Endpoint response, the requests library is imported alongside Given, When, then decorators from the behave library.
import requests
from behave import given, when, then
@given('the API endpoint is "{endpoint}"')
def step_api_endpoint(context, endpoint):
context.url = endpoint
@when('I make a GET request')
def step_get_request(context):
context.response = requests.get(context.url)
@then('the response status should be "{status_code}"')
def step_response_status(context, status_code):
assert context.response.status_code == int(status_code), f'Expected status code {status_code}, but got {context.response.status_code}'
@then('the response should contain the field "{field}"')
def step_response_contains_field(context, field):
try:
json_data = context.response.json()
except Exception:
json_data = {}
# Optional: allow for failed responses that have no field
if context.response.status_code == 404:
assert field not in json_data or not json_data.get(field), f"Unexpected field '{field}' found in 404 response"
else:
assert field in json_data, f"Field '{field}' not found in response"
Code Walkthrough:
Here, you must create an environment.py for setup and teardown, which will contain the code responsible for connecting your local script to a cloud testing platform.
The reason for executing tests over the cloud is that the tests may struggle with maintaining multiple browser versions, OS configurations, and devices, leading to flaky tests and inconsistent results.
Local setups are slow, resource-heavy, and hard to scale for large test suites. Cloud testing solves these issues, enabling reliable Behave testing at scale across thousands of environments.
As a cloud testing platform, will use LambdaTest as the execution platform, which allows you to run Behave test automation at scale across 3000+ browsers and OS combinations.
To get started with LambdaTest, follow these steps:
context.browser = webdriver.Remote(
command_executor=f"https://{username}:{authkey}@hub.lambdatest.com/wd/hub",
options=options
)
This setup ensures that your Behave tests run reliably at scale, without worrying about local environment inconsistencies or resource limitations.
Code Implementation:
The environment.py file allows you to define functions that execute at specific points during your BDD tests with Python Behave. Setup and teardown logic is defined here. You should also import all dependencies required for the project setup and teardown in this file.
from behave.model_core import Status
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox options import Options as FirefoxOptions
from selenium.webdriver.edge.options import Options as EdgeOptions
import os
import json
INDEX = int(os.environ['INDEX']) if 'INDEX' in os.environ else 0
if os.environ.get("env") == "jenkins":
desired_cap_dict = os.environ["LT_BROWSERS"]
CONFIG = json.loads(desired_cap_dict)
else:
json_file = "config/config.json"
with open(json_file) as data_file:
CONFIG = json.load(data_file)
username = os.environ["LT_USERNAME"]
authkey = os.environ["LT_ACCESS_KEY"]
def before_scenario(context, scenario):
try:
desired_cap = setup_desired_cap(CONFIG[INDEX])
if 'Chrome' in scenario.tags:
options = ChromeOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 11"
elif 'Firefox' in scenario tags:
options = FirefoxOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 10"
elif 'Edge' in scenario tags:
options = EdgeOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 8"
else:
raise ValueError("Unsupported browser tag")
options.set_capability('build', desired_cap.get('build'))
options.set_capability('name', desired_cap.get('name'))
print("Browser Options:", options.to_capabilities())
context.browser = webdriver.Remote(
command_executor=f"https://{username}:{authkey}@hub.lambdatest.com/wd/hub",
options=options
)
except Exception as e:
print(f"Error in before_scenario: {str(e)}")
context.scenario.skip(reason=f"Failed to initialize browser: {str(e)}")
def after_scenario(context, scenario):
if hasattr(context, 'browser'):
try:
if scenario.status == Status.failed:
context.browser.execute_script("lambda-status=failed")
else:
context.browser.execute_script("lambda-status=passed")
except Exception as e:
print(f"Error setting lambda status: {str(e)}")
finally:
context.browser.quit()
def setup_desired_cap(desired_cap):
cleaned_cap = {}
for key, value in desired_cap.items():
if key == 'connect':
if not isinstance(value, (int, float)) or value is None:
cleaned_cap[key] = None
else:
cleaned_cap[key] = value
else:
cleaned_cap[key] = value
return cleaned_cap
Code Walkthrough:
Running Python Behave Tests
To run Python behave tests, ensure that you’re in the root directory of your project (the one containing the `features` directory) and run the following command in the terminal:
behave
Output on the local terminal:
Result on the LambdaTest automation dashboard:
To learn how to configure and run your automation testing scripts on LambdaTest, follow this support documentation on the Python framework Behave.
Running Specific Feature Files in Python Behave:
To run specific feature files, in your terminal, specify the path to the exact feature file you want executed. To execute only the api_status.feature file, run the command below in the terminal:
behave features/api_status.feature
Running Specific Test Scenarios With Tags:
Tags allow you to run only specific scenarios that have been tagged. For example, you can run a scenario with the @Chrome tag using the command:
behave --tags=@Chrome
Running Python Behave in Parallel:
To experience faster execution of Behave tests, you’ll have to run your tests in parallel. By default, Behave does not provide parallel execution; hence, the need to use the Behave wrapper framework, BehaveX. It is noticeably used for executing parallel test sessions, among other things.
To execute your Python Behave tests using 4 parallel processes, follow the command below:
behavex --parallel-processes 4
The --parallel-process flag is used to indicate that the tests should be executed in parallel. The number 4 indicates that 4 parallel processes should be used to run the tests.
In the case your path length exceeds 108 bytes and there is an exception when executing in parallel, use the command below:
TMPDIR=/tmp behavex --parallel-processes 4
Another capability of BehaveX is the ability to generate a report. The report is in three types: HTML, JUnit, and JSON. To create a report, use the default behavex command to execute tests, and reports will be generated.
Run the following commands to generate, locate and view the report.
behavex
/path/to/behavior_driven_development_python_behave/output/report.html
TMPDIR=/tmp behavex --output-folder my_reports
Use the --output-folder flag, followed by the name of the folder, and your reports will be created in the given name folder, in this case, it's my_reports.
When writing and troubleshooting Python Behave tests, it’s helpful to follow practices inspired by Cucumber, another BDD framework.
Many Cucumber best practices, such as using clear Gherkin syntax, organizing step definitions logically, and maintaining consistent feature file structures, also apply to Python Behave, ensuring reliable, maintainable, and readable BDD tests.
Some of the common issues are mentioned below:
This issue often occurs if the behave executable file is not installed or found in the PC’s PATH environment variables.
Solution:
pip install behave --upgrade --force-reinstall
The most common cause of the Behave command not recognized issue results from an unverified installation of Behave or non-activation of the Python virtual environment.
When the Behave folder structure is not followed strictly when setting up a project, issues with the directory structure will arise.
Solution:
paths=features
Behave follows a strict folder structure; hence, any deviation from the expected structure results in directory structure issues.
Step definition errors occur when Behave is unable to map feature files to their corresponding Python code implementations. A warning is often displayed by Behave on the terminal about undefined steps when these step definition errors occur.
Solution:
@then('the response status should be {status_code}')
def step_status_code(context, status_code):
pass
Missing import statements, undefined steps, and syntax errors are the usual suspects for step definition errors encountered when running Behave.
The issue of crashes and unexpected behavior often occurs in CI/CD pipelines because of the Unix-based environments.
Solution:
When executing BDD tests with Python Behave in CI/CD pipelines, there are chances that there will be crashes and unexpected behavior, as seen here.
Behavior-driven development, as a software development methodology, is increasingly being adopted by developers and organizations due to its focus on communication among technical and non-technical members on a project. It helps everyone on the team understand and outline the desired behavior of an application or feature using human-readable language.
For Python-based projects, a recommended framework for executing BDD is Behave. A Behave project structure is easy to set up and get started with. It uses the Gherkin language for writing software use case scenarios in plain English with its syntax and keywords. Creating step definitions in Python files is how Behave maps each step written in Python executable code to the related Gherkin language scenarios.
When the behave keyword is used to run the BDD tests, the feature files in the features repository are discovered along with the associated step definitions for execution. For additional capabilities, such as report generation and parallel execution, BehaveX is used.
Did you find this page helpful?
More Related Hubs
Start your journey with LambdaTest
Get 100 minutes of automation test minutes FREE!!