Behave - 步骤匹配器



Behave 中有三种类型的步骤匹配器。它们解释如下:

  • ParseMatcher (parse) - 基于 parse 模块。

  • extended ParseMatcher (cfparse) - 允许基数语法。

  • RegexMatcher (re) - 基于正则表达式进行模式匹配。

Parse 匹配器

这是内置的步骤匹配器,具有以下特性:

  • 简单易用和理解。

  • 预定义和用户定义的数据类型支持此匹配器。

  • 借助数据类型重新利用正则表达式。

  • 隐藏正则表达式的复杂性。

扩展的 Parse 匹配器

它扩展了 Parse 匹配器。除了 Parse 匹配器的功能外,它还具有其他功能。

附加功能包括:

  • 理解基数字段语法。

  • 为具有基数字段部分的字段生成缺失的类型转换器。

  • 基于 parse-type。

Regex 匹配器

它具有以下特性:

  • 向后兼容 Cucumber。

  • 比 parse 匹配器更容易使用。

让我们详细了解 parse 匹配器。

Parse 匹配器

特性文件中可能存在具有几乎相似短语的步骤。Behave 具有解析能力。为此使用use_step_parser方法,我们必须将解析器类型作为参数传递给该方法。

对于 parse 匹配器,我们必须传递参数 parse。它利用 parse 进行正则表达式解析和匹配。

特性文件(几乎相同的 Given 步骤)

相似步骤的特性文件如下:

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

相应的步骤实现文件

步骤实现文件如下:

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

输出

运行特性文件后获得的输出如下所示。这里,我们使用了命令behave --no-capture -f plain

Step Matchers

输出显示debitcredit。这两个值已通过特性文件中几乎相同的 Given 步骤传递。在步骤实现中,我们已解析这两个步骤。

广告
© . All rights reserved.