簡體   English   中英

如何編寫基於先前功能的 Cucumber 測試?

[英]How Do I Write Cucumber Tests That Build On Previous Features?

我已經開始使用 Cucumber 來編寫 BDD 測試以匹配我的應用程序的業務用例。 它是基於區塊鏈的,因此測試中的每個用戶都在運行應用程序的一個實例。 這也意味着每次測試都非常繁重,95% 的測試時間都在設置階段。

當我編寫測試時,我發現我開始重復自己,早期的功能似乎變得多余了。

一種業務流程是:

  • User1 保存一條新消息
  • User2 編輯消息
  • User1 驗證編輯
  • User1 取消消息
  • User2 確認取消

這被分解為新/編輯/取消功能。

最初,我從“新建”和“編輯”功能文件開始,如下所示:

新的

Feature: a new message is added

  Scenario: a user adds a new message
    Given there is a user called User1
    And there is a user called User2
    When User1 creates a new message with id 1234
    Then User2 should see the message with id 1234

編輯

Feature: Editing a message

  Scenario: A User edits a message
    Given there is a user called User1
    And there is a user called User2
    When User1 creates a new message with id 1234
    And User2 adds the location US to the message
    Then User1 should see the location US on the message

但是現在我要進入取消部分,我意識到為了正確測試取消,系統需要有一個編輯過的消息,這意味着我需要通過新建和編輯功能才能將消息放入正確的 state .

這將使取消看起來像這樣,然后開始變得很長:

取消

Feature: Cancelling a message

  Scenario: A User cancels a message
    Given there is a user called User1
    And there is a user called User2
    When User1 creates a new message with id 1234
    And User2 adds the location US to the message
    And User1 cancels the message
    Then User2 should see status Cancelled on the message

我可以這樣寫取消:

Feature: Cancelling a message

  Scenario: A User cancels a message
    Given there is a message with id 1234
    And User1 cancels the message
    Then User2 should see status Cancelled on the message

它作為一個功能讀起來很好,但是,我現在必須為“有一條 id 為 1234 的消息”編寫一個步驟定義,它可以完成編輯功能所做的一切。

如開頭所述,這些測試中的設置需要 95% 的測試時間,因此理想情況下,我希望將這些作為一系列步驟一起運行,而不是從每個功能的全新開始。 例如

  • 進行一次設置
  • 創建新消息
  • 編輯消息
  • 取消消息

是否可以將場景或功能鏈接在一起並重用上一個系統 state?

還是我每次都必須從頭開始啟動系統?

一個步驟定義是否調用了構成編輯功能的所有其他步驟/方法以正確的方式調用 go 用於取消,或者我應該寫很多 And 語句?

我可以理解您對此的不滿,但沒有辦法讓后續功能相互構建。 每個場景都意味着是原子的和可重復的。 場景相互依賴的問題是一個失敗的場景將導致后續場景中的級聯故障。 應用程序中的一次失敗會觸發多次失敗的測試,導致您的團隊開始認為測試是不穩定的。

編寫一個模擬先前場景的步驟並沒有錯——這是正確的做法。 在定義這些步驟時,請盡可能保持它們的原子性,以便它們非常可組合。

老實說,一個 6 步的場景非常好。 我建議的唯一更改是制作您的步驟的Given版本。 取消場景看起來有很多When

Feature: Cancelling a message

  Scenario: A User cancels a message
    Given there is a user called User1
    And there is a user called User2
    And User1 created a new message with id 1234
    And User2 added the location US to the message
    When User1 cancels the message
    Then User2 should see status Cancelled

原文:您可以將代碼從每個設置步驟中提取出來並使其發揮作用,將函數作為步驟的代碼調用(執行與以前相同的任務),並創建一個調用這些函數的新設置步驟,這意味着他們會有一個共享的實現

替代方法:編寫一個標記的 Before 鈎子,它知道被測系統的 state ,即使它只是設置步驟是否發生,並使用該信息為這些場景重置系統。 您甚至可以在此處執行運行狀況檢查,以確保在系統需要時可以進行完全重置。

或者甚至將這段代碼自己放入步驟中,並讓他們知道如果健康檢查已通過則跳過部分(很可能是 if 語句)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM