Behave - Gherkin 關鍵字



以下是 Behave 中的 Gherkin 關鍵字:

  • 特性 (Features)

  • 場景 (Scenario)

  • 步驟 (Steps)

  • 背景 (Background)

  • 場景大綱 (Scenario Outline)

  • 文字 (Text)

  • 表格 (Table)

  • 標籤 (Tags)

  • 假如 (Given)

  • 當 (When)

  • 那麼 (Then)

  • 但是 (But)

  • 並且 (And)

特性檔案是用 Gherkin 語言編寫的。它是純文字,由團隊的非技術成員(業務分析師)建立。特性檔案可用於自動化測試和文件。

Behave 包含行尾結束語句。我們可以使用製表符/空格進行縮排。大多數行以關鍵字開頭,例如 Scenario、Given、Then 等。可以在檔案的任何位置添加註釋。它們以空格開頭/不以空格開頭,後跟 # 符號以及文字。

讓我們討論一些關鍵的 Gherkin 關鍵字。

特性 (Feature)

一個特性包含多個場景。它們可能包含/不包含描述、背景和一組標籤。

特性檔案的結構如下:

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

特性的名稱應該對正在測試的特性進行描述。但是,冗長的描述不是強制性的,只在特性名稱不明確時才新增描述。

背景 (Background)

新增背景是為了擁有一組步驟。它類似於一個場景。我們可以使用背景為多個場景新增上下文。它在特性中每個場景之前執行,但在執行 before 鉤子之後。

背景通常用於執行前提條件,例如登入場景或資料庫連線等。

可以新增背景描述以提高人類可讀性。背景在一個特性檔案中只能出現一次,並且必須在場景或場景大綱之前宣告。

不應使用背景來建立複雜狀態(除非無法避免)。此部分應簡短且準確。此外,我們應避免在一個特性檔案中包含大量場景。

包含背景的特性檔案

包含 Background 關鍵字的特性檔案如下:

Feature: Payment Process
   Background:
      Given launch application
      Then Input credentials
   Scenario: Credit card transaction
      Given user is on credit card payment screen
      Then user should be able to complete credit card payment
   Scenario: Debit card transaction
      Given user is on debit card payment screen
      Then user should be able to complete debit card payment

場景 (Scenario)

場景定義了正在測試的應用程式的行為。它有一個標題來描述其目標。可以新增其描述以提高人類可讀性。

場景可能有多個步驟,這些步驟以關鍵字 Given、Then、When 等開頭。建議使用場景來檢查單個特性或預期結果。

包含場景的特性檔案

包含 Scenario 關鍵字的特性檔案如下:

Feature − Payment Process
      Scenario − Credit card transaction
   Given user is on credit card payment screen
   Then user should be able to complete credit card payment

場景大綱 (Scenario Outline)

如果我們有一組類似的標準和要在場景中傳遞的結果,則使用場景大綱。場景大綱附帶一個示例表格,並且可以有多個示例表格。

對於示例表中標題行後的每一行,測試都會執行一次。要測試的值由括號 <> 中的名稱表示。這些名稱應與示例表標題匹配。

它有助於減少程式碼行數,因為它消除了重複的步驟並整理了我們的測試。

包含場景大綱的特性檔案

包含 Scenario Outline 關鍵字的特性檔案如下:

Feature − User information
Scenario Outline: Check login functionality
   Given user enters <email> and <password>
   Then user should be logged in

示例

以下是包含 Scenario Outline 的特性檔案的示例:

Examples: Credentials
   | email        | password  |
   | qa@gmail.com | pwd1      |
   | qe@gmail.com | pwd2      |

相同的測試將使用不同的引數集執行。

假如 (Given)

以關鍵字 Given 開頭的步驟用於在使用者與系統互動之前將系統置於熟悉的環境中(類似於前提條件)。建議不要在 Given 步驟中描述使用者操作。

可以新增 Given 步驟來設定資料庫中的配置、登入應用程式等。

包含 Given 的特性檔案

包含 Given 關鍵字的特性檔案如下:

Feature − Payment Process
            Scenario − Credit card transaction
   Given user is on credit card payment screen
   Then user should be able to complete credit card payment

當 (When)

以關鍵字 When 開頭的步驟用於新增使用者要執行的基本任務。透過此步驟,使用者與系統進行互動,從而導致系統狀態發生變化或對其他地方產生影響。

包含 When 的特性檔案

包含 When 關鍵字的特性檔案如下:

Feature − Payment Process
            Scenario − Credit card transaction
   Given user is on credit card payment screen
      When user clicks on the Payment with Credit Card button
   Then user should be able to complete credit card payment

那麼 (Then)

以關鍵字 Then 開頭的步驟用於獲取預期結果。此步驟中觀察到的結果(理想情況下以輸出形式 - 訊息、報告等)應與業務場景和它所在的特性檔案相關聯。

建議不要將 Then 步驟用於資料庫場景,因為它主要用於描述終端使用者可見的後果。

包含 Then 的特性檔案

包含 When 關鍵字的特性檔案如下:

Feature − Payment Process
            Scenario − Credit card transaction
   Given user is on credit card payment screen
   When user clicks on the Payment with Credit Card button
   Then user should be able to complete credit card payment

And、But

如果我們有多個連續的 Given、When、Then 步驟,我們可以使用 And 和 But 步驟。它提高了使用者的可讀性。

包含多個連續 Then/Given 步驟的特性檔案

Behave 中包含多個連續 Then/Given 步驟的特性檔案如下:

Feature − Verify book names added in Library
   Scenario − Verify Book name
      Given Book1 details
      Given Book2 details
      Then Verify book names
      Then Verify newly added book names should not be in Delete History

不包含多個 Then/Given 步驟的特性檔案

不包含多個 Then/Given 步驟的特性檔案如下:

Feature − Verify book names added in Library
   Scenario − Verify Book name
      Given Book1 details
      And Book2 details
      Then Verify book names
         But Verify newly added book names should not be in Delete History

步驟資料 - 表格

步驟可以具有與其關聯的文字和資料表。我們可以向步驟新增資料表。建議縮排表格資料,並且必須為每一行設定相等的列數。

列資料應以 | 符號分隔。

包含表格的特性檔案

包含 table 關鍵字的特性檔案如下:

Feature − User Registration
Scenario − User enters registration details
   When User enters name and password
      | name |password |
      | t1   | pwd     |
      | t2   | pwd1    |
Then user should be able to complete registration

表格可透過上下文變數 (傳遞到步驟函式) 中的 .table 屬性訪問實現 Python 程式碼。表格是 Table 的一個例項。

表格的實現邏輯

以下是 Table 中 .table 屬性的實現邏輯:

@when('User enters name and password')
def step_impl(context):
   for r in context.table:
      model.delete_usr(name=r['name'], password=r['password'])

步驟資料 - 文字

步驟後用 """ 括起來的文字塊將與該步驟連結。此處,將解析縮排。開頭的所有空格都將從文字中刪除。此外,所有後續行必須至少具有與起始行相同的最小空格。

文字可透過上下文變數 (傳遞到步驟函式) 中的 .text 屬性訪問實現 Python 程式碼。

包含文字的特性檔案

包含 text 關鍵字的特性檔案如下:

Feature − Verify book name added in Library
   
   Scenario − Verify Book name
      Given Book details
         """
          Text added for a step
         """
      Then Verify book name

標籤 (Tags)

可以為特性檔案的某個部分新增標籤,以便 Behave 能夠僅驗證特性檔案的特定部分。只能為場景、特性、場景大綱新增標籤。

此外,用於特性的標籤將由其所有場景和場景大綱繼承。標籤放在我們想要標記的場景或特性之前。我們也可以在一行中用空格分隔多個標籤。標籤以 @ 開頭,後跟標籤名稱。

包含標籤的特性檔案

包含 tags 關鍵字的特性檔案如下:

@payment
@high
Feature − Payment Process
      Scenario − Credit card transaction
   Given user is on credit card payment screen
   Then user should be able to complete credit card payment

標籤透過根據標籤排除/包含特定場景或特性來幫助管理測試執行。

廣告
© . All rights reserved.