Skillversitas

Enumeration in Steps

When a step only makes sense with a closed set of words, behave lets you list the allowed values inside a single placeholder using the pipe character. The pattern {one|two|three} matches exactly those three words and nothing else, so unrelated step text is not silently accepted.

Basic Enumeration

Scenario: Natural quantities
  Given I have two cucumbers
  When I eat one cucumber
  Then I have one cucumber left
from behave import given, when, then

WORDS = {"one": 1, "two": 2, "three": 3}

@given("I have {one|two|three} cucumbers")
def step_impl(context, word):
    context.cucumbers = WORDS[word]

@when("I eat {one|two|three} cucumbers")
def step_impl(context, word):
    context.cucumbers -= WORDS[word]

@then("I have {one|two|three} cucumbers left")
def step_impl(context, word):
    assert context.cucumbers == WORDS[word]

Multiple Enumerations in One Step

Each placeholder in the same step can have its own enumeration. This is handy for steps that pair two restricted values, such as an action and a currency:

from behave import step

@step("I {deposit|withdraw} {10|20|50} {USD|EUR}")
def step_impl(context, action, amount, currency):
    delta = int(amount)
    if action == "withdraw":
        delta = -delta
    context.balance[currency] = context.balance.get(currency, 0) + delta

Benefit over Plain Parameters

A plain {word} placeholder matches any word and forces you to validate inside the function. An enumeration moves that validation into the pattern: an unmatched step text is reported as an unmatched step, which is exactly the feedback you want when a business rule changes.

Summary

Enumerations constrain a placeholder to a fixed list of words. The captured word is still a string, so map it to the real value (a number, a flag, a constant) inside the step body.