我已經開始使用 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 語句?
查看完整描述