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.