Behave - 可选部分



功能文件中可能存在具有几乎相同短语的步骤。Behave 具有解析能力,因此一个步骤定义可以涵盖这些步骤。为此使用use_step_parser方法,并且我们必须将解析器类型作为参数传递给该方法。

对于扩展的解析匹配器,我们必须传递参数 cfparse。它具有基数字段 (CF) 支持。默认情况下,它会为连接的基数生成缺失的类型转换器(如果给出了等于一的基数的类型转换器)。

它可以支持以下解析表达式:

  • {values:Type+} – 基数=1..N,多

  • {values:Type*} – 基数=0..N,多0

  • {values:Type?} – 基数=0..1,可选

功能文件(几乎相同的步骤)

具有几乎相同步骤的功能文件如下:

Feature − Payment Process
Scenario − Check Debit transactions
      Given user is on "debit" screen
   Scenario − Check Credit transactions
      Given user is on "credit" screen

register_type 方法用于注册用户定义的类型,该类型可以在匹配步骤时用于任何类型转换。

相应的步骤实现文件

步骤实现文件如下:

from behave import *
import parse
#define parse type
use_step_matcher("cfparse")
# for whitespace characters
@parse.with_pattern(r"x\s+")
def parse_string(s):
#type converter for "x" succeeded by single/multiple spaces
   return s.strip()
#register user-defined datatype
register_type(x_=parse_string)
#optional part :x_? cardinality field in parse expression
@given('user is on {:x_?}{payment} screen')
def step_payment(context, x_, payment):
   print("Payment type: ")
   print(payment)

输出

运行功能文件后获得的输出如下所示,使用的命令为behave --no-capture -f plain

Optional Part

输出显示debitcredit。这两个值已在功能文件中以几乎相同的步骤传递。在步骤实现中,我们使用解析表达式中的基数字段解析了这两个步骤。

广告
© . All rights reserved.