Behave step matcher for Cucumber Expressions.
Project description
Behave Cucumber Matcher
Behave step matcher for Cucumber Expressions.
Installation
behave-cucumber-matcher
is available via PyPI:
pip install behave-cucumber-matcher
Usage
Import and patch the matcher into Behave inside environment.py
in your features
directory.
from behave.matchers import use_step_matcher, matcher_mapping
from behave_cucumber_matcher import build_step_matcher
from cucumber_expressions.parameter_type_registry import ParameterTypeRegistry
# Initialise a Cucumber Expressions parameter registry
parameter_registry = ParameterTypeRegistry()
# Create the step matcher to pass to behave
step_matcher = build_step_matcher(parameter_registry)
# Patch the step matcher into behave
matcher_mapping["cucumber_expressions"] = step_matcher
# Specify to use the Cucumber Expressions step matcher
use_step_matcher("cucumber_expressions")
Create a scenario inside color.feature
in 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
in your features/steps
directory:
from behave import given, then, when
from cucumber_expressions.parameter_type import ParameterType
from environment import parameter_registry
# Define the parameter type
color = ParameterType(
name="color",
regexp="red|blue|yellow",
type=str,
transformer=lambda s: s,
use_for_snippets=True,
prefer_for_regexp_match=False,
)
# 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 features
Feature: Color selection # features/Gherkin.feature:1
Rule: User can select a profile color
Scenario: User selects a valid color # features/Gherkin.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.
Acknowledgements
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 and direct import of the matcher at package level rather than via its module.
License
behave-cucumber-matcher
is licensed under the MIT License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for behave_cucumber_matcher-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0013636d9967bfb830081d7c3c03dd5e3fcb9723900349f4330a82051b818260 |
|
MD5 | c9a294de35128dd475fd4ab53ebb0645 |
|
BLAKE2b-256 | 312bf397b685367b06237f806fd789c5dc449e9f6abdaacde56c8c8884bcdfb3 |
Hashes for behave_cucumber_matcher-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b647a88e32a84745396c92f7ac52da026a83cdb0377f39f6524462b7d94a937 |
|
MD5 | fc3e6223b7576deca36cc497c2a2705c |
|
BLAKE2b-256 | bc057640ee9d4120463d75caccaa7b6109473e41af208f74c9884a01860bc901 |