Behave – Step Matchers

Behave - Step Matchers

There are three types of in Behave Step Matchers. There are −

  • ParseMatcher (parse) − Based on the parse module.
  • extended ParseMatcher(cfparse) − Allows cardinality syntax.
  • RegexMatcher (re) − Based on regular expressions for matching patterns.

Types of Behave Step Matchers –

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 the 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’s describe parse matches

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 matches, we have to pass the parameter parse. It utilizes the parse for regular expressions parsing and matching.

Feature File (almost Given similar steps)

The feature file for 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

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.

Behave - Step Matchers

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 steps.

Next Topic – Click Here

This Post Has One Comment

Leave a Reply