亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何編寫基于以前功能的 Cucumber 測試?

如何編寫基于以前功能的 Cucumber 測試?

婷婷同學_ 2023-10-12 14:50:13
我已經開始使用 Cucumber 編寫 BDD 測試來匹配我的應用程序的業務用例。它基于區塊鏈,因此測試中的每個用戶都在運行應用程序的一個實例。這也意味著每次測試都相當繁重,95%的測試時間都在準備階段。當我編寫測試時,我發現我開始重復自己,并且早期的功能似乎變得多余。一種業務流程是:用戶1保存一條新消息用戶2編輯消息User1 驗證編輯用戶1取消消息用戶2確認取消這分為新建/編輯/取消功能。最初,我從“新建”和“編輯”功能文件開始,如下所示:新的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但現在我進入取消部分,我意識到為了正確測試取消,系統需要編輯消息,這意味著我需要完成新建和編輯功能以使消息進入正確的狀態。這將使取消看起來像這樣,然后開始變得相當冗長:取消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% 的測試時間,因此理想情況下,我希望將這些作為一系列步驟一起運行,而不是從每個功能的新鮮開始。例如進行一次設置創建新消息編輯消息取消留言是否可以將場景或功能鏈接在一起并重用前一個場景或功能的系統狀態?還是每次都必須從頭開始啟動系統?調用構成“編輯”功能的所有其他步驟/方法的步驟定義是否是“取消”的正確方法,還是應該編寫大量 And 語句?
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

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

編寫一個模擬之前場景的步驟并沒有什么問題——這是正確的方法。定義這些步驟時,請盡可能保持它們的原子性,以便它們非??山M合。

老實說,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


查看完整回答
反對 回復 2023-10-12
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

原始:您可以從每個設置步驟中提取代碼并使它們起作用,將函數作為步驟的代碼調用(執行與之前相同的任務),然后創建一個調用這些函數的新設置步驟,這意味著他們有一個共享的實現

替代方案:編寫一個帶標記的 Before 掛鉤,它知道被測系統的狀態,即使只是設置步驟是否已發生,并使用該信息針對這些情況重置系統。您甚至可以在此處執行運行狀況檢查,以確保在系統需要時可以進行完全重置。

或者甚至將此代碼放入步驟本身中,并讓他們知道如果運行狀況檢查已通過則跳過部分(很可能是 if 語句)


查看完整回答
反對 回復 2023-10-12
  • 2 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號