Behavior‑Driven Development (BDD) helps teams collaborate by expressing application behavior in a language everyone understands—developers, testers, and business stakeholders alike. In the Python ecosystem, Behave is a popular BDD framework that follows the Cucumber / Gherkin syntax, enabling readable, executable specifications. In this post, we’ll walk through setting up Behave in Visual Studio Code, understand the recommended folder structure, and write our first Cucumber-style test, all using free and open‑source tooling.
What is Behave?
Behave is a BDD test framework for Python that allows you to write tests in plain English using Given–When–Then syntax, backed by Python step definitions.
Key benefits:
- Human‑readable test scenarios using Gherkin
- Strong alignment between business requirements and test automation
- Easy integration with CI/CD pipelines
- Lightweight and IDE‑friendly
Prerequisites
Before getting started, ensure you have the following installed:
- Python 3.10+
- Visual Studio Code
- Basic understanding of Python
- Familiarity with BDD concepts (Given / When / Then)
Steps
Download the sample demo zip from github download
Step 1: Create a Virtual Environment and activate it.
python -m venv venv
.venv\Scripts\activate
Install Dependencies
pip install behave requests
Step 2: Install VS Code Extensions
To get a first‑class experience in VS Code, install the following extensions:
- Python (Microsoft)
- Gherkin (for .feature syntax highlighting)
- Behave VSC (optional but recommended)
The Behave VSC extension enables:
- Running tests directly from VS Code
- Step definition navigation
- Gherkin auto‑completion
- Test explorer integration
Folder Structure
Why This Structure?
- features/ – contains all Gherkin feature files
- steps/ – contains Python step implementations
- environment.py – optional hooks for setup/teardown
- config/configuration.py - for lifecycle hooks
- behave.ini – configuration file for Behave
Step 3: Write Your First Feature File
Feature: Login functionality
Login
Scenario: Successful login
Given the application is running
When the user enters valid credentials
Then the user should see the dashboard
Step 4: Writing Step Definitions
from behave import given, when, then
@given('the user is on the login page')
def step_user_on_login_page(context):
print("User navigates to login page")
@when('the user enters valid credentials')
def step_user_enters_credentials(context):
print("User enters username and password")
@then('the user should be redirected to the dashboard')
def step_user_redirected(context):
print("User is redirected to dashboard")
Step 5: Adding Test Configuration (configuration.py)
Create config/configuration.py to centralize environment-specific settings. This helps avoid hardcoding values across test files.
class TestConfig:
BASE_URL = "https://example.com"
TIMEOUT = 30
BROWSER = "chrome"
Step 6: Using Fixtures with environment.py
The environment.py file is Behave’s hook mechanism. It runs before and after tests, similar to fixtures in pytest.
Create features/environment.py:
from config.configuration import TestConfig
def before_all(context):
print("Setting up test environment")
context.config_data = TestConfig()
def before_scenario(context, scenario):
print(f"Starting scenario: {scenario.name}")
def after_scenario(context, scenario):
print(f"Finished scenario: {scenario.name}")
def after_all(context):
print("Tearing down test environment")
Common Use Cases
- Initialize browsers or API clients
- Load environment variables
- Clean up test data
- Open/close DB connections
Step 7: Optional Behave Configuration File
Create behave.ini for execution settings. This helps during debugging by showing logs directly in the console.
[behave]
stdout_capture = false
stderr_capture = false
log_capture = false
Step 8: Running Tests
From the project root, run:
behave
To run a specific feature:
behave features/login.feature
Run by tag
behave -t Login
Best Practices
✔ Keep feature files business-readable
✔ Avoid logic in feature files
✔ Reuse steps wherever possible
✔ Centralize configs and fixtures
✔ Use tags for selective execution