Behavior-driven development testing framework for Python using Gherkin syntax
npx @tessl/cli install tessl/pypi-behave@1.3.0A comprehensive Behavior-Driven Development (BDD) testing framework for Python that enables teams to write tests in natural language style using the Gherkin syntax, backed by Python step implementations. It facilitates collaboration between developers, QA engineers, and non-technical stakeholders by allowing test scenarios to be written in plain English using Given-When-Then statements.
pip install behaveimport behaveFor step definitions:
from behave import given, when, then, step
# Or uppercase aliases:
from behave import Given, When, Then, StepFor fixtures and step matchers:
from behave import fixture, use_fixture, register_type, use_step_matcherAdditional imports for fixtures:
from behave.fixture import use_fixture_by_tag# steps/tutorial.py
from behave import given, when, then
@given('we have behave installed')
def step_impl(context):
pass
@when('we implement a test')
def step_impl(context):
assert True is not False
@then('behave will test it for us!')
def step_impl(context):
assert context.failed is FalseFeature file (tutorial.feature):
Feature: showing off behave
Scenario: run a simple test
Given we have behave installed
When we implement a test
Then behave will test it for us!Running tests:
behaveBehave follows the BDD pattern with clear separation of concerns:
Core decorators for defining test steps that match Gherkin statements. These form the foundation of BDD test implementation, allowing natural language scenarios to be backed by executable Python code.
def given(pattern: str, **kwargs): ...
def when(pattern: str, **kwargs): ...
def then(pattern: str, **kwargs): ...
def step(pattern: str, **kwargs): ...
# Uppercase aliases:
def Given(pattern: str, **kwargs): ...
def When(pattern: str, **kwargs): ...
def Then(pattern: str, **kwargs): ...
def Step(pattern: str, **kwargs): ...Reusable setup and teardown functionality for managing test state, database connections, browser instances, and other resources. Fixtures can be scoped to functions, scenarios, features, or the entire test session.
def fixture(func=None, *, scope: str = "function", name: str = None): ...
def use_fixture(fixture_func, context, *args, **kwargs): ...
def use_fixture_by_tag(tag, context, fixture_registry): ...Pattern matching and parameter conversion system for extracting and transforming data from step text into Python objects. Supports multiple matching strategies and custom type registration.
def register_type(name: str, func): ...
def use_step_matcher(name: str): ...
def use_default_step_matcher(name: str = None): ...
# Deprecated:
def step_matcher(name: str): ... # Use use_step_matcher() insteadCore classes representing BDD constructs including features, scenarios, steps, and execution context. These provide the structural foundation for organizing and running BDD tests.
class Feature: ...
class Scenario: ...
class Step: ...
class Context: ...
class Runner: ...Command-line interface and configuration system for customizing test execution, output formatting, test selection, and runtime behavior. Supports extensive options for different testing environments.
class Configuration: ...
def main(argv: list = None) -> int: ...Extensible reporting system supporting multiple output formats including plain text, JSON, JUnit XML, and custom formats. Enables integration with CI/CD systems and test reporting tools.
class Formatter: ...
class PlainFormatter(Formatter): ...
class PrettyFormatter(Formatter): ...
class JSONFormatter(Formatter): ...
class JUnitFormatter(Formatter): ...