Behave Overview
Behave is a behavior-driven development (BDD) framework for Python. It lets teams describe application behavior in natural, structured language that both technical and non-technical stakeholders can read, then connects those descriptions to executable Python code. The written scenarios become living documentation and an automated acceptance test suite at the same time.
What Behave Provides
Behave follows the classic BDD workflow popularized by Cucumber: scenarios are written in Gherkin, a plain-text language with keywords such as Feature, Scenario, Given, When, and Then. Behave parses those files, matches each step to a registered Python function, and executes them as tests. Its main components are:
- Feature files (with the
.featureextension) that hold scenarios written in Gherkin. - Step definitions in Python modules decorated with
@given,@when, and@then. - environment.py, a hook file for setup and teardown at every level of the run.
- behave.ini and command-line options to configure paths, tags, formatters, and logging.
- A runner that collects features, executes them, and produces reports.
How a Run Works
A typical behave project keeps all feature files in a directory called features/ and step modules under features/steps/. When you invoke behave, it scans the feature directory, groups scenarios by feature, applies tag filtering, runs hooks, and executes each scenario independently. State carries between the steps of one scenario through the context object and is wiped afterwards, which keeps every scenario self-contained.
Feature: Account balance
Scenario: Withdraw money
Given a bank account with 100 USD
When the account holder withdraws 30 USD
Then the remaining balance is 70 USD
from behave import given, when, then
@given("a bank account with 100 USD")
def step_account(context):
context.balance = 100
@when("the account holder withdraws 30 USD")
def step_withdraw(context):
context.balance -= 30
@then("the remaining balance is 70 USD")
def step_check(context):
assert context.balance == 70
Key Strengths
Behave is mature, dependency-light, and designed around the standard Python ecosystem. It integrates naturally with unittest, pytest-style helpers, Selenium, requests, and any test double library. Its notable strengths include:
- Gherkin step parameters, data tables, and scenario outlines for data-driven tests.
- Tag-based selection such as
--tags=@wipto focus on subsets of work. - Multiple output formatters: plain, pretty, JSON, HTML, and progress.
- Flexible hooks (
before_all,before_scenario,after_scenario,before_step, and more). - Internationalized Gherkin keywords for many spoken languages.
- A step retry mechanism for handling flaky operations.
When to Use Behave
Choose behave when conversations about requirements matter: product owners write or review scenarios, and developers make them executable. It fits end-to-end acceptance testing, API integration testing, and UI automation. If your tests are purely technical and no stakeholder reads them, a plain framework like pytest may suffice; the value of behave comes from shared, readable specifications.
Summary
Behave turns Gherkin scenarios into running Python tests through step definitions, a context object, hooks, and configurable formatters. The next chapters walk through installation, writing your first feature, implementing steps, and the advanced features that make suites maintainable at scale.