Behave - 鉤子函式



Behave 的設定和拆卸函式在一個名為 environment.py 的檔案中實現,該檔案與包含步驟資料夾的同一目錄中。

設定函式包括:開啟瀏覽器、資料庫連線、配置等等。

拆卸函式包括:關閉瀏覽器、終止資料庫連線、撤銷更改等等。

  • environment.py 檔案包含以下函式:

  • before_feature(context, feature) - 在每個特性之前執行。

  • before_scenario(context, scenario) - 在每個場景之前執行。

  • before_step(context, step) - 在每個步驟之前執行。

  • before_tag(context, tag) - 在每個標籤之前執行。

  • before_all(context) - 在所有內容之前執行。

  • after_feature(context, feature) - 在每個特性之後執行。

  • after_scenario(context, scenario) - 在每個場景之後執行。

  • after_step(context, step) - 在每個步驟之後執行。

  • after_tag(context, tag) - 在每個標籤之後執行。

after_all(context) - 在所有內容之後執行。

Project Structure

以上函式在 Behave 中用作鉤子函式。專案結構應如下所示:

包含鉤子函式的特性檔案 (Payment.feature)

Feature − Payment Process
Scenario − Verify transactions
         Given user makes a payment of 100 INR And user makes a payment of 10 Dollar

包含 Payment.feature 鉤子函式的特性檔案如下:

包含鉤子函式的特性檔案 (Payment1.feature)

Feature − Administration Process
Scenario − Verify admin transactions
         Given user is on admin screen

Payment1.feature 的包含鉤子函式的特性檔案如下:

相應的步驟實現檔案

from behave import *
from parse_type import TypeBuilder
parse_amt = TypeBuilder.make_choice(["100", "10"])
register_type(Amt=parse_amt)
parse_curr = TypeBuilder.make_choice(["INR", "Dollar"])
register_type(Curn=parse_curr)
@given("user makes a payment of {n:Amt} {t:Curn}")
def step_payment(context, n, t):
   pass
@given('user is on admin screen')
def step_admin(context):
   pass

步驟實現檔案如下:

步驟 4 - environment.py 檔案中的鉤子函式

# before all
def before_all(context):
   print('Before all executed')
# before every scenario
def before_scenario(scenario, context):
   print('Before scenario executed')
# after every feature
def after_feature(scenario, context):
   print('After feature executed')
# after all
def after_all(context):
   print('After all executed')

environment.py 檔案中的鉤子函式如下:

輸出

Hooks
列印頁面
© . All rights reserved.