Behave step matcher for Cucumber Expressions.
behave-cucumber-matcher
is available via PyPI:
pip install behave-cucumber-matcher
Import and specify the matcher inside environment.py
of your features
directory.
from behave.matchers import use_step_matcher
from behave_cucumber_matcher import CUCUMBER_EXPRESSIONS_MATCHER
# Specify to use the Cucumber Expressions step matcher
use_step_matcher(CUCUMBER_EXPRESSIONS_MATCHER)
Create a scenario inside color.feature
of your features
directory:
Feature: Color selection
Rule: User can select a profile color
Scenario: User selects a valid color
Given I am on the profile settings page
When I select the theme colour "red"
Then the profile colour should be "red"
Create step definitions inside color.py
of your features/steps
directory:
from behave import given, then, when
from behave_cucumber_matcher import parameter_registry
from cucumber_expressions.parameter_type import ParameterType
# Define the parameter type
color = ParameterType(
name="color",
regexp="red|blue|yellow",
type=str,
)
# Pass the parameter type to the registry instance
parameter_registry.define_parameter_type(color)
@given("I am on the profile customisation/settings page")
def step_given(context):
assert True
# Reference the parameter type in the step definition pattern
@when('I select the theme colo(u)r "{color}"')
def step_when(context, selected_color):
assert selected_color
context.selected_color = selected_color
@then('the profile colo(u)r should be "{color}"')
def step_then(context, displayed_color):
assert displayed_color
assert context.selected_color == displayed_color
The necessary files are now in place to execute your gherkin scenario.
repository/
└── features/
├── steps/
│ └── color.py
├── environment.py
└── color.feature
Finally, execute Behave. The scenario will run with the step definitions using the Cucumber Expressions parameter type.
$ behave
Feature: Color selection # features/color.feature:1
Rule: User can select a profile color
Scenario: User selects a valid color # features/color.feature:5
Given I am on the profile settings page # features/steps/color.py:20 0.000s
When I select the theme colour "red" # features/steps/color.py:26 0.000s
Then the profile colour should be "red" # features/steps/color.py:32 0.000s
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s
For detailed usage of behave, see the official documentation.
Based on the Behave step matcher base class and built on the architecture of cuke4behave by Dev Kumar Gupta, with extended type hints, a fix for detecting patterns without arguments, a default parameter type registry, additional documentation for arguments and return types, direct import of the matcher at package level rather than via its module, backwards compatibility with Cucumber Expressions missing parameter type defaults, and a global parameter type registry.
behave-cucumber-matcher
is licensed under the MIT License