Skip to main content

Behave step matcher for Cucumber Expressions.

Project description

Behave Cucumber Matcher

Release version License Python versions Supported platforms Ruff Code style: black Imports: isort Pipeline status

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

behave_cucumber_matcher-0.1.0.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

behave_cucumber_matcher-0.1.0-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page