Skillversitas

Step Parameters

Step parameters let one step definition match many concrete steps by capturing values from the step text. Behave uses named placeholders in curly braces such as {num} or {name}; the captured value is passed to the step function as a keyword argument with the placeholder's name.

Defining a Parameterized Step

The feature file contains the real value; the step definition contains the placeholder. Captured values always arrive as strings, so convert them when needed.

Scenario: Buying groceries
  Given I have 5 cucumbers in my belly
  When I eat 2 cucumbers
  Then I have 3 cucumbers left
from behave import given, when, then

@given("I have {num} cucumbers in my belly")
def step_impl(context, num):
    context.cucumbers = int(num)

@when("I eat {num} cucumbers")
def step_impl(context, num):
    context.cucumbers -= int(num)

@then("I have {num} cucumbers left")
def step_impl(context, num):
    assert context.cucumbers == int(num), (
        f"expected {num}, got {context.cucumbers}"
    )

Using Context to Share State

Parameters are arguments, not storage. To remember a value between steps, attach it to the context object, as in the example above. That is the standard pattern for building up state across a scenario: earlier steps write to the context, later steps read from it.

Multiple Parameters

A single step definition may declare several placeholders. Each captured value is passed as a keyword argument under its own name:

When I transfer 10 USD from "alice" to "bob"
from behave import when

@when('I transfer {amount} USD from "{src}" to "{dst}"')
def step_impl(context, amount, src, dst):
    amount = float(amount)
    context.wallet[src] -= amount
    context.wallet[dst] += amount

Summary

Curly-brace placeholders turn one step definition into a family of steps. Captured values are always strings, so convert them explicitly, and store anything you need later on the context object.