CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-behave

Behavior-driven development testing framework for Python using Gherkin syntax

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

step-definitions.mddocs/

Step Definitions

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.

Capabilities

Given Step Decorator

Decorator for defining Given steps that describe the initial context or setup for a scenario. Given steps establish the pre-conditions before testing begins.

def given(pattern: str, use_step_matcher: str = None):
    """
    Decorator for defining Given steps in BDD scenarios.
    
    Parameters:
    - pattern: str, regular expression or parse pattern to match step text
    - use_step_matcher: str, optional step matcher to use ("parse", "cfparse", "re")
    
    Returns:
    Function decorator that registers the step definition
    """

Usage example:

@given('I have {count:d} items in my basket')
def step_impl(context, count):
    context.basket_items = count

When Step Decorator

Decorator for defining When steps that describe actions or events. When steps represent the behavior being tested.

def when(pattern: str, use_step_matcher: str = None):
    """
    Decorator for defining When steps in BDD scenarios.
    
    Parameters:
    - pattern: str, regular expression or parse pattern to match step text
    - use_step_matcher: str, optional step matcher to use ("parse", "cfparse", "re")
    
    Returns:
    Function decorator that registers the step definition
    """

Usage example:

@when('I remove {count:d} items from my basket')
def step_impl(context, count):
    context.basket_items -= count

Then Step Decorator

Decorator for defining Then steps that describe expected outcomes or assertions. Then steps verify that the system behaves as expected.

def then(pattern: str, use_step_matcher: str = None):
    """
    Decorator for defining Then steps in BDD scenarios.
    
    Parameters:
    - pattern: str, regular expression or parse pattern to match step text
    - use_step_matcher: str, optional step matcher to use ("parse", "cfparse", "re")
    
    Returns:
    Function decorator that registers the step definition
    """

Usage example:

@then('I should have {count:d} items in my basket')
def step_impl(context, count):
    assert context.basket_items == count

Generic Step Decorator

Generic step decorator that can match any step type (Given, When, or Then). Useful for reusable steps that apply to multiple contexts.

def step(pattern: str, use_step_matcher: str = None):
    """
    Generic step decorator for steps that can match any type.
    
    Parameters:
    - pattern: str, regular expression or parse pattern to match step text
    - use_step_matcher: str, optional step matcher to use ("parse", "cfparse", "re")
    
    Returns:
    Function decorator that registers the step definition
    """

Usage example:

@step('I wait {seconds:d} seconds')
def step_impl(context, seconds):
    time.sleep(seconds)

Alternative Step Decorators

Behave also provides uppercase aliases for the step decorators for stylistic preferences.

# Uppercase aliases (identical functionality to lowercase versions)
def Given(pattern: str, use_step_matcher: str = None): ...
def When(pattern: str, use_step_matcher: str = None): ...
def Then(pattern: str, use_step_matcher: str = None): ...
def Step(pattern: str, use_step_matcher: str = None): ...

Usage example:

from behave import Given, When, Then

@Given('I have an account')
def step_impl(context):
    context.account = Account()

@When('I log in')
def step_impl(context):
    context.account.login()

@Then('I should be authenticated')
def step_impl(context):
    assert context.account.is_authenticated()

Async Step Support

Behave natively supports async step functions since version 1.4.0. Simply use the regular step decorators with async functions.

Usage example:

@when('I make an async API call')
async def step_impl(context):
    response = await context.http_client.get('/api/data')
    context.response = response

Step Function Context

All step functions receive a context parameter that provides shared state and utilities:

Context Object

class Context:
    """
    Execution context shared between steps within a scenario.
    
    Attributes:
    - feature: Current Feature object
    - scenario: Current Scenario object  
    - table: Table data attached to current step (if any)
    - text: Multi-line text attached to current step (if any)
    - failed: Boolean indicating if any step has failed
    - config: Configuration object with behave settings
    - tags: Set of tags from current scenario and feature
    """

The context object allows steps to:

  • Share data between steps in a scenario
  • Access step data (tables and multi-line text)
  • Check execution status
  • Access configuration settings
  • Use tag-based conditional logic

Step Data

Steps can access additional data attached to them in feature files:

Table Data

# In feature file:
# When I process the following data:
#   | name  | age |
#   | Alice | 25  |
#   | Bob   | 30  |

@when('I process the following data')
def step_impl(context):
    for row in context.table:
        name = row['name']
        age = int(row['age'])
        # Process row data

Multi-line Text

# In feature file:
# When I submit the following JSON:
#   """
#   {"user": "test", "action": "login"}
#   """

@when('I submit the following JSON')
def step_impl(context):
    import json
    data = json.loads(context.text)
    # Process JSON data

Install with Tessl CLI

npx tessl i tessl/pypi-behave

docs

configuration.md

execution-model.md

fixtures.md

formatters.md

index.md

matchers-types.md

step-definitions.md

tile.json