Introduction
Behave brings behavior-driven development to Python using the Gherkin language. Product managers and QA engineers write test scenarios in plain English .feature files using Given/When/Then syntax, while developers implement the matching step definitions in Python. This separation keeps specifications readable and tests executable.
What Behave Does
- Parses Gherkin .feature files written in Given/When/Then natural language syntax
- Maps Gherkin steps to Python functions via decorator-based step definitions
- Supports scenario outlines for data-driven testing with example tables
- Provides hooks (before/after scenario, feature, step) for setup and teardown
- Generates JUnit XML output for CI integration and custom formatters for reporting
Architecture Overview
Behave reads .feature files from a features directory, parses them into an internal scenario model, and matches each Gherkin step against registered Python step definitions using regex or parse patterns. The context object passes state between steps within a scenario. Environment hooks in environment.py run at feature, scenario, and step boundaries for setup and cleanup.
Self-Hosting & Configuration
- Install via pip; works with Python 3.7 and later
- Place .feature files in a
features/directory at the project root - Implement step definitions in
features/steps/as Python files - Configure behave via
behave.ini,.behaverc, orpyproject.toml - Use
environment.pyfor shared fixtures, browser setup, or database connections
Key Features
- Gherkin syntax that non-technical stakeholders can read and validate
- Step definition reuse across multiple features and scenarios
- Scenario outlines with Examples tables for parameterized testing
- Tag-based filtering to run smoke, regression, or WIP test subsets
- Active formatters for JUnit XML, JSON, progress bars, and custom output
Comparison with Similar Tools
- pytest-bdd — BDD plugin for pytest; Behave is a standalone framework with its own runner
- Cucumber — The original BDD framework (Ruby/Java); Behave is the Python equivalent
- Robot Framework — Keyword-driven; Behave uses Gherkin natural language syntax
- Lettuce — Older Python BDD tool; Behave is actively maintained with a larger community
- pytest — Code-centric assertions; Behave separates specification from implementation
FAQ
Q: Can Behave run alongside pytest? A: Yes. You can use pytest-behave or simply run behave as a separate command in your CI pipeline alongside pytest.
Q: Does it support async step definitions?
A: Behave's step runner is synchronous. For async code, use asyncio.run() within step definitions or consider pytest-bdd with pytest-asyncio.
Q: How do I share state between steps?
A: Use the context object. It is passed to every step function and persists within a scenario. Context attributes set in one step are available in subsequent steps.
Q: Can I generate HTML reports?
A: Yes. Use the --format json output with tools like Allure or behave-html-formatter to produce rich HTML reports.