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.