[英]Cucumber migrate scenario to scenario outline
我想將多行場景移動到場景大綱,以便可以將表的各個行作為單獨的測試進行測試。 這是我想到的一個簡化示例:
Scenario: This scenario loops through the lines of the table performing an assert on each line
When I do something and verify it
| name | parameter1 | parameter2 | parameter3 |
| A and 1 | A | 1 | true |
| B and 1 | B | 1 | false |
| A and 2 | A | 2 | false |
| B and 2 | B | 2 | true |
步驟定義如下所示:
@When("I do something and verify it")
public void doSomethingAndVerifyIt(DataTable dataTable) {
List<Map<String, String>> keyValues = dataTable.asMaps();
for (Map<String, String> keyValue : keyValues) {
assertSomething(keyValue.get("parameter1"), keyValue.get("parameter2"), keyValue.get("parameter3"));
}
}
這工作正常,但如果任何行未能通過斷言步驟,則測試在此時停止。 我想將其更改為沿這些線使用場景大綱,以便這些線可以相互獨立地通過或失敗:
Scenario Outline: This scenario loops through the lines of the table performing an assert on each line
When I do something and verify it
Examples:
| name | parameter1 | parameter2 | parameter3 |
| A and 1 | A | 1 | true |
| B and 1 | B | 1 | false |
| A and 2 | A | 2 | false |
| B and 2 | B | 2 | true |
如何更改步驟定義,以便每次測試讀取一行? 我知道我可以通過在按名稱顯式聲明每個參數的步驟定義下添加一行來做到這一點,但在我的情況下,這將涉及大量參數。
有沒有更簡單的方法來做到這一點:
@When("I do something and verify it")
public void doSomethingAndVerifyIt(Map<String, String> keyValue) {
assertSomething(keyValue.get("parameter1"), keyValue.get("parameter2"), keyValue.get("parameter3"));
}
有關如何使用 table 的信息,請參閱我的其他答案。
改進場景的更好方法是將示例轉換為命名規則。 在討論行為時從業務中獲取信息時,示例非常有用。 然而,在編寫代碼時,它們並不是很好。 讓我們用密碼登錄來探索一下。
說我們有
password | success?
123456 | no
xb32drthcyfe | no
p@ssword1 | yes
作為我們的例子。 我們在這里遇到的問題是我們不知道為什么xb32drthcyfe
應該失敗而p@ssword1
應該成功。 我們可以猜測,但我們的場景未能記錄 WHY。
現在考慮
Scenario: Passwords must contain a symbol
When I register with a password without a symbol
Then I should see my password needs a symbol
現在,您有一個場景,不僅可以記錄 WHY,還可以挑戰 WHY,並探索 WHY,例如
Scenario: Weak password that contains a symbol
Given my password is long enough has a symbol but it is really weak
When I register with my password
Then I should be registered.
現在您可以向您的企業展示上述行為並說,我們真的想這樣做嗎?
每個示例背后都應該有一個唯一的命名規則。 為了讓示例變得足夠成熟以駐留在您的 cukes 中,您需要揭示此規則,為其命名,然后將示例在您的 cukes 中的用法替換為探索規則的場景。
您必須在步驟 defs 中使用列標題作為參數,以便黃瓜可以用表中的值替換參數,例如
Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.