Behave - Step Matchers



There are three types of Step Matchers in Behave. They are explained below −

  • ParseMatcher (parse) − Based on the parse module.

  • extended ParseMatcher(cfparse) − Allows cardinality syntax.

  • RegexMatcher (re) − Based on regular expressions for matching patterns.

Parse matcher

It is the in-built step matcher which has the below mentioned features:

  • Simple to use and comprehend.

  • Predefined and user-defined data types support this matcher.

  • Re-utilises regular expressions with the help of data types.

  • Conceals the complexity of regular expression.

extended Parse matcher

It extends the Parse Matcher. It has additional features along with the features of Parse matcher.

The additional features include −

  • Comprehends the cardinality field syntax.

  • Generates missing type converters for the fields with cardinality field parts.

  • Built on parse-type.

Regex matcher

It has the below features −

  • Backward compatible to Cucumber.

  • Easier to use compared to a parse matcher.

Let us understand the parse matchers in detail.

Parse Matchers

There are maybe steps in the feature file having almost similar phrases. Behave has the parsing ability. The method use_step_parser is used for this and we have to pass the parser type as a parameter to that method.

For parse matchers, we have to pass the parameter parse. It utilises the parse for regular expressions parsing and matching.

Feature File - payment.feature

The feature file for the similar steps is as follows −

Feature: Payment Process
Scenario: Check Debit transactions
      Given user is on "debit" screen
      When user makes a payment
Scenario: Check Credit transactions
      Given user is on "credit" screen

Corresponding Step Implementation File - stepImpPayment.py

The step implementation file is as follows −

from behave import *

#define parser type
use_step_matcher("parse")
@given('user is on "{p}" screen')
def step_impl(context, p):
   print(p)
@when('user makes a payment')
def step_pay_complete(context):
   pass

Output

The output obtained after running the feature file is mentioned below. Here, we have used the command behave --no-capture -f plain.

(myenv) D:\behave\myenv\pythonProject>behave --no-capture -f plain
USING RUNNER: behave.runner:Runner
Feature: Payment Process

  Scenario: Check Debit transactions
debit
    Given user is on "debit" screen ... passed in 0.000s
    When user makes a payment ... passed in 0.000s

  Scenario: Check Credit transactions
credit
    Given user is on "credit" screen ... passed in 0.000s

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped
Took 0min 0.000s

The output shows debit and credit. These two values have been passed with almost similar Given steps in the feature file. In step implementation, we have parsed both the steps.

Advertisements