Behavior-driven development testing framework for Python using Gherkin syntax
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core classes representing BDD constructs including features, scenarios, steps, and execution context. These provide the structural foundation for organizing and running BDD tests.
Represents a feature file containing scenarios, background steps, and feature metadata. Features are the top-level containers in BDD testing.
class Feature:
"""
Container for scenarios, background, and feature metadata.
Attributes:
- name: str, feature name from feature file
- description: str, feature description text
- scenarios: list, collection of Scenario objects
- background: Background, common steps for all scenarios (optional)
- tags: list, feature-level tags
- filename: str, path to feature file
- language: str, natural language used (default: "en")
- line: int, line number where feature starts
Methods:
- run(runner): Execute the feature with given runner
- should_run(config): Check if feature should run based on configuration
"""Represents a scenario or scenario outline with steps and metadata. Scenarios define specific test cases within a feature.
class Scenario:
"""
Container for steps and scenario metadata.
Attributes:
- name: str, scenario name
- description: str, scenario description text
- steps: list, collection of Step objects
- tags: list, scenario-level tags (includes feature tags)
- feature: Feature, parent feature object
- line: int, line number where scenario starts
- status: Status, execution status (passed/failed/skipped/undefined)
- skip_reason: str, reason for skipping (if skipped)
Methods:
- run(runner): Execute the scenario with given runner
- should_run(config): Check if scenario should run based on tags/configuration
"""Represents a parameterized scenario with example tables. Scenario outlines enable data-driven testing with multiple parameter combinations.
class ScenarioOutline:
"""
Parameterized scenario with example tables.
Attributes:
- name: str, scenario outline name
- description: str, scenario outline description
- steps: list, template Step objects with parameters
- examples: list, Example objects containing parameter tables
- tags: list, scenario outline tags
- feature: Feature, parent feature object
Methods:
- run(runner): Execute all example combinations
- build_scenarios(): Generate individual scenarios from examples
"""Represents an individual Given/When/Then step in a scenario with associated data and execution status.
class Step:
"""
Individual Given/When/Then step.
Attributes:
- step_type: str, step type ("given", "when", "then", "and", "but")
- name: str, step text without step keyword
- text: str, multi-line text attached to step (optional)
- table: Table, tabular data attached to step (optional)
- line: int, line number where step appears
- status: Status, execution status
- duration: float, execution time in seconds
- error_message: str, error details if step failed
- match: Match, matched step definition details
Methods:
- run(runner, context): Execute the step
- should_run(config): Check if step should run
"""Represents background steps that run before each scenario in a feature. Background provides common setup steps.
class Background:
"""
Common steps executed before each scenario.
Attributes:
- name: str, background name (optional)
- description: str, background description
- steps: list, collection of Step objects
- line: int, line number where background starts
Methods:
- run(runner, context): Execute background steps
"""Execution context object that provides shared state and utilities for steps. The context is passed to every step function and hook.
class Context:
"""
Shared state and utilities for steps within a scenario.
Attributes:
- feature: Feature, current feature being executed
- scenario: Scenario, current scenario being executed
- table: Table, table data from current step (if any)
- text: str, multi-line text from current step (if any)
- failed: bool, whether any step in scenario has failed
- config: Configuration, behave configuration object
- tags: set, combined tags from feature and scenario
- aborted: bool, whether execution was aborted
- log_capture: StringIO, captured log output
- stdout_capture: StringIO, captured stdout
- stderr_capture: StringIO, captured stderr
Methods:
- execute_steps(steps_text): Execute additional steps dynamically
- add_cleanup(cleanup_func): Register cleanup function
"""Main test execution engine that processes features and runs scenarios and steps.
class Runner:
"""
Main test runner that executes features and scenarios.
Attributes:
- config: Configuration, behave configuration
- features: list, features to execute
- hooks: dict, registered hook functions
- formatters: list, output formatters
- step_registry: StepRegistry, registered step definitions
Methods:
- run(): Execute all features and return success status
- run_feature(feature): Execute a single feature
- run_scenario(scenario): Execute a single scenario
- run_step(step): Execute a single step
- setup_capture(): Initialize output capture
- teardown_capture(): Finalize output capture
"""Represents tabular data attached to steps, providing structured access to table rows and columns.
class Table:
"""
Tabular data attached to steps.
Attributes:
- headings: list, column headers
- rows: list, Row objects representing table data
Methods:
- assert_equals(expected_table): Assert table equality
- require_column(column_name): Ensure column exists
- require_columns(column_names): Ensure multiple columns exist
"""Represents a single row in a step table with column access methods.
class Row:
"""
Single row in a step table.
Attributes:
- cells: list, raw cell values
- headings: list, column headers
Methods:
- get(column, default=None): Get value by column name
- as_dict(): Convert row to dictionary
- __getitem__(column): Get value by column name or index
"""Represents scenario and feature tags used for filtering and organization.
class Tag:
"""
Tag metadata for filtering and organization.
Attributes:
- name: str, tag name (without @ symbol)
- line: int, line number where tag appears
"""Execution status constants for steps, scenarios, and features.
class Status:
"""
Execution status enumeration.
Values:
- passed: Step/scenario executed successfully
- failed: Step/scenario failed with error
- skipped: Step/scenario was skipped
- undefined: Step definition not found
- executing: Currently executing (internal use)
"""The execution model integrates with behave's comprehensive hook system:
def before_all(context):
"""Called once before all features"""
def before_feature(context, feature):
"""Called before each feature"""
def before_scenario(context, scenario):
"""Called before each scenario"""
def before_step(context, step):
"""Called before each step"""
def after_step(context, step):
"""Called after each step"""
def after_scenario(context, scenario):
"""Called after each scenario"""
def after_feature(context, feature):
"""Called after each feature"""
def after_all(context):
"""Called once after all features"""def before_tag(context, tag):
"""Called for scenarios with specific tags"""
def after_tag(context, tag):
"""Called after scenarios with specific tags"""@when('I process the following data')
def step_impl(context):
if context.table:
for row in context.table:
name = row['name']
value = row['value']
# Process table data
if context.text:
# Process multi-line text
lines = context.text.split('\n')@given('I initialize the test environment')
def step_impl(context):
context.test_data = {}
context.resources = []
@when('I perform an action')
def step_impl(context):
# Access shared state
result = perform_action(context.test_data)
context.result = result
@then('the result should be correct')
def step_impl(context):
assert context.result == expected_value@when('I execute a complex workflow')
def step_impl(context):
# Execute additional steps dynamically
context.execute_steps('''
Given I have valid credentials
When I authenticate with the service
Then I should be authenticated
''')Install with Tessl CLI
npx tessl i tessl/pypi-behave