Behave - 步驟實現



Behave 特性檔案中場景的步驟應該具有用 Python 編寫的實現邏輯。這被稱為實現/步驟定義檔案(.py 副檔名),應該位於 steps 目錄中。

此檔案中包含所有必要的匯入。steps 目錄應屬於 features 目錄的一部分。

您的計算機上將顯示以下螢幕:

Step Implementations

步驟定義檔案包含定義特性檔案中步驟的 Python 函式。在 Python 函式的開頭,必須使用以 @given、@when 等開頭的裝飾器。這些裝飾器會與特性檔案中的 Given、Then、When 和其他步驟進行比較和匹配。

特性檔案

特性檔案如下:

Feature − Verify book name added in Library
   Scenario − Verify Book name
      Given Book details
      Then Verify book name

對應的步驟實現檔案

對應的步驟實現檔案如下所示:

from behave import *
@given('Book details')
def impl_bk(context):
      print('Book details entered')
@then('Verify book name')
def impl_bk(context):
      print('Verify book name')

輸出

執行特性檔案後獲得的輸出如下:

Corresponding Step Implementation File

輸出顯示特性和場景名稱,以及測試結果和測試執行持續時間。

廣告
© . All rights reserved.