【发布时间】:2020-07-11 09:42:30
【问题描述】:
我想将多行场景移动到场景大纲中,以便可以将表格的各个行作为单独的测试进行测试。这是我想到的一个简化示例:
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"));
}
【问题讨论】:
标签: java datatable cucumber scenarios