[英]Run each cucumber test independently
我在日食上使用黃瓜和行家,而我想做的是獨立運行每個測試。 例如,我有一個圖書館系統軟件,該軟件基本上允許用戶借書和做其他事情。
條件之一是用戶最多只能借兩本書,因此我寫信來確保該功能正常運行。 這是我的功能文件:
Scenario: Borrow over max limit
Given "jim@help.ca" logs in to the library system
When "jim@help.ca" order his first book with ISBN "9781611687910"
And "jim@help.ca" orders another book with ISBN "9781442667181"
And "jim@help.ca" tries to order another book with ISBN "1234567890123"
Then jim will get the message that says "The User has reached his/her max number of books"
我寫了一個相應的步驟定義文件,每個文件都很好。 但是,將來我想使用相同的用戶名(“ jim@help.ca”)借書,好像jim@help.ca尚未借任何書。 我希望每個測試彼此獨立。
有什么方法可以做到...也許我可以在步驟定義類中添加一些內容,例如拆解方法。 我已經對其進行了調查,但無法對其提供任何可靠的信息。 如果有辦法請幫助我。 非常感謝您的幫助,在此先感謝您!
是的,您可以在每種情況之前和之后進行設置和拆卸,但是它不在步驟定義文件中。 您要使用的是鈎子。
掛鈎可以在方案之前或之后運行,並且可以在每個方案之前或之后運行,也可以僅在您和@tag之前運行。
@remove_borrowed_books
Scenario: Borrow over max limit
不幸的是,我只使用了紅寶石黃瓜而不是Java,所以我無法為您提供分步說明,但這應該告訴您您需要了解的內容https://zsoltfabok.com/blog/2012/09/cucumber- JVM-鈎/
您可以使用“ @After”鈎子來實現此目的,就像@Derek提到的那樣,例如使用每個用戶名借用的圖書地圖:
private final Map<String, Integer> booksBorrowed = new HashMap<>();
@After
public void tearDown() {
booksBorrowed.clear();
}
@Given("...")
public void givenUserBorrowsBook(String username) {
booksBorrowed.put(username, booksBorrowed.containsKey(username) ? booksBorrowed.get(username) + 1 : 1);
....
}
或使用“ @Before”掛鈎在執行每個方案之前執行清理,這是我建議的選項:
private Map<String, Integer> booksBorrowed;
@Before
public void setUp() {
booksBorrowed = new HashMap<>();
}
如果計划並行運行方案,則邏輯將更加復雜,因為您將需要維護執行特定方案的線程與該線程上使用的用戶名之間的關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.