Use the Regular Expression Matcher (re)

This step matcher allows to use regular expressions in step definition. The named parameter syntax (?P<name>...) should be used to extract parameters from the step definition.

See also

Regular Expressions for more information on regular expressions.

Simple Parameters

Provide the Step Definitions

# file:step_matcher.features/steps/step_re_matcher_simple_param.py
# -----------------------------------------------------------------------------
# STEPS: With "re" matcher
# -----------------------------------------------------------------------------
from behave import use_step_matcher, when
use_step_matcher("re")

# -- SIMPLE GROUP: foo
@when(u'I try to match "(?P<foo>foo)"')
def step_when_I_try_to_match_foo(context, foo):
    context.foo = foo

# -- SIMPLE GROUP: bar
@when(u'I try to match "(?P<bar>bar)"')
def step_when_I_try_to_match_bar(context, bar):
    context.bar = bar

# -- SIMPLE GROUP: anything else
@when(u'I try to match "(?P<anything>.*)"')
def step_when_I_try_to_match_anything_else(context, anything):
    context.anything = anything
# file:step_matcher.features/steps/step_parse_matcher.py
# -----------------------------------------------------------------------------
# MORE STEPS: With "parse" matcher
# -----------------------------------------------------------------------------
use_step_matcher("parse")

@given(u'I use the regular expression step matcher')
def step_given_I_use_regex_matcher(context):
    pass

@then(u'the parameter "{name}" is "{expected_value}"')
def step_then_parameter_is_equal_to(context, name, expected_value):
    actual_value = getattr(context, name, None)
    assert_that(actual_value, equal_to(expected_value))

@then(u'the parameter "{name}" is none')
def step_then_parameter_is_none(context, name):
    actual_value = getattr(context, name, None)
    assert_that(actual_value, is_(none()))

Run the Test

Now we run this example with behave:

$ behave --no-source ../step_matcher.features/re_matcher.simple_param.feature
Feature: Use "re" Step Matcher with Simple Parameters

  Scenario: Match simple parameter 
    Given I use the regular expression step matcher
    When I try to match "foo"
    Then the parameter "foo" is "foo"
    And the parameter "bar" is none

  Scenario: Match two simple parameters 
    Given I use the regular expression step matcher
    When I try to match "foo"
    And I try to match "bar"
    Then the parameter "foo" is "foo"
    And the parameter "bar" is "bar"

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
9 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

Optional Parameters

Provide the Step Definitions

# file:step_matcher.features/steps/step_re_matcher_optional_param.py
# -----------------------------------------------------------------------------
# STEPS: With "re" matcher
# -----------------------------------------------------------------------------
from behave import use_step_matcher, when
use_step_matcher("re")

# -- OPTIONAL 1: Optional param is captured and provided as parameter "an_".
@when(u'I try to match (?P<an_>an )?optional "(?P<foo>foo)"')
def step_when_I_try_to_match_an_optional_foo(context, an_, foo):
    context.foo = foo
    context.an_ = an_

# -- OPTIONAL 2: Optional param is matched, but not captured.
@when(u'I try to match (?:an )?optional "(?P<bar>bar)"')
def step_when_I_try_to_match_an_optional_bar(context, bar):
    context.bar = bar

Run the Test

Now we run this example with behave:

$ behave --no-source ../step_matcher.features/re_matcher.optional_param.feature
Feature: Use "re" Step Matcher with Optional Parameters

  Scenario: Optional parameter 1 is missing 
    Given I use the regular expression step matcher
    When I try to match optional "foo"
    Then the parameter "foo" is "foo"
    And the parameter "an_" is none

  Scenario: Optional parameter 1 is provided 
    Given I use the regular expression step matcher
    When I try to match an optional "foo"
    Then the parameter "foo" is "foo"
    And the parameter "an_" is "an "

  Scenario: Optional parameter 2 is missing 
    Given I use the regular expression step matcher
    When I try to match optional "bar"
    Then the parameter "bar" is "bar"

  Scenario: Optional parameter 2 is provided (not captured) 
    Given I use the regular expression step matcher
    When I try to match an optional "bar"
    Then the parameter "bar" is "bar"

1 feature passed, 0 failed, 0 skipped
4 scenarios passed, 0 failed, 0 skipped
14 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

Nested Parameters

Provide the Step Definitions

# file:step_matcher.features/steps/step_re_matcher_nested_param.py
# -----------------------------------------------------------------------------
# STEPS: With "re" matcher
# -----------------------------------------------------------------------------
from behave import use_step_matcher, when
use_step_matcher("re")

# -- NESTED GROUP:
@when(u'I try to match nested "(?P<foo>foo(?P<bar>bar)?)"')
def step_when_I_try_to_match_nested_foobar(context, foo, bar):
    context.foo = foo
    context.bar = bar

# -- SIMPLE GROUP: anything else
@when(u'I try to match nested "(?P<anything>.*)"')
def step_when_I_try_to_match_anything_else(context, anything):
    context.anything = anything

Run the Test

Now we run this example with behave:

$ behave ../step_matcher.features/re_matcher.nested_param.feature
Feature: Use "re" Step Matcher with Nested Parameters   # ../step_matcher.features/re_matcher.nested_param.feature:1

  Scenario: Nested parameter with nested match      # ../step_matcher.features/re_matcher.nested_param.feature:3
    Given I use the regular expression step matcher # ../step_matcher.features/steps/step_parse_matcher.py:12
    When I try to match nested "foobar"             # ../step_matcher.features/steps/step_re_matcher_nested_param.py:14
    Then the parameter "foo" is "foobar"            # ../step_matcher.features/steps/step_parse_matcher.py:16
    And the parameter "bar" is "bar"                # ../step_matcher.features/steps/step_parse_matcher.py:16
    And the parameter "anything" is none            # ../step_matcher.features/steps/step_parse_matcher.py:21

  Scenario: Nested parameter with nested anything-else match  # ../step_matcher.features/re_matcher.nested_param.feature:10
    Given I use the regular expression step matcher           # ../step_matcher.features/steps/step_parse_matcher.py:12
    When I try to match nested "foo bar"                      # ../step_matcher.features/steps/step_re_matcher_nested_param.py:20
    Then the parameter "foo" is none                          # ../step_matcher.features/steps/step_parse_matcher.py:21
    And the parameter "bar" is none                           # ../step_matcher.features/steps/step_parse_matcher.py:21
    And the parameter "anything" is "foo bar"                 # ../step_matcher.features/steps/step_parse_matcher.py:16

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
10 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s