黄瓜将方案迁移到方案大纲

时间:2020-03-30 22:16:05

标签: java datatable cucumber scenarios

我想将多行方案移至方案大纲,以便可以将表的各行作为单独的测试进行测试。这是我所想到的一个简化示例:

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"));
}

2 个答案:

答案 0 :(得分:0)

有关如何使用表格的信息,请参见我的其他答案。

改善方案的更好方法是将示例转换为命名规则。 在讨论行为时从业务中获取信息时,示例非常有用。但是,在编写代码时,它们并不是很好。让我们用密码登录来探讨一下。

说我们有

password     | success?

123456       |   no
xb32drthcyfe |   no
p@ssword1    |   yes

作为我们的示例。这里存在的问题是我们不知道为什么xb32drthcyfe应该失败而p@ssword1应该成功。我们可以猜测,但是我们的方案未能记录为什么。

现在考虑

Scenario: Passwords must contain a symbol
 When I register with a password without a symbol
 Then I should see my password needs a symbol

现在您有一个场景,不仅可以记录为什么,而且还可以挑战为什么,并探索为什么,例如

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.

现在您可以向您的企业介绍上述行为,然后说,我们真的要这样做吗?

每个示例后面都应有一个唯一的命名规则。为了使示例变得足够成熟以驻留在库克中,您需要显示此规则,为其命名,然后用探索规则的方案替换库中示例的用法。

答案 1 :(得分:0)

您必须在步骤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 |