简体   繁体   English

黄瓜迁移场景到场景大纲

[英]Cucumber migrate scenario to scenario outline

I want to move a multi-row scenario to a scenario outline so that the individual lines of the table can be tested as separate tests.我想将多行场景移动到场景大纲,以便可以将表的各个行作为单独的测试进行测试。 Here's a simplified example of what I have in mind:这是我想到的一个简化示例:

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       |

The step definition looks like this:步骤定义如下所示:

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

This works fine, but if any of the rows fails the assertion step, then the test stops at this point.这工作正常,但如果任何行未能通过断言步骤,则测试在此时停止。 I'd like to change this to using a scenario outline along these lines so that the lines can pass or fail independently of each other:我想将其更改为沿这些线使用场景大纲,以便这些线可以相互独立地通过或失败:

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       |

How do I change the step definition so that each time, one row is read by the test?如何更改步骤定义,以便每次测试读取一行? I know that I could do this by adding a line under the step definition that explicitly declares each parameter by name, but in my case this would involve a huge number of parameters.我知道我可以通过在按名称显式声明每个参数的步骤定义下添加一行来做到这一点,但在我的情况下,这将涉及大量参数。

Is there a simpler way to do this along these lines:有没有更简单的方法来做到这一点:

@When("I do something and verify it")
public void doSomethingAndVerifyIt(Map<String, String> keyValue) {
    assertSomething(keyValue.get("parameter1"), keyValue.get("parameter2"), keyValue.get("parameter3"));
}

See my other answer for how to use table.有关如何使用 table 的信息,请参阅我的其他答案。

A better way to improve your scenario is to convert your examples to named rules.改进场景的更好方法是将示例转换为命名规则。 Examples are very useful when capturing information from the business when discussing behaviour.在讨论行为时从业务中获取信息时,示例非常有用。 However they are not very good when it comes to writing code.然而,在编写代码时,它们并不是很好。 Lets explore this a bit with password login.让我们用密码登录来探索一下。

Say we have说我们有

password     | success?

123456       |   no
xb32drthcyfe |   no
p@ssword1    |   yes

as our examples.作为我们的例子。 The problem we have here is we don't know WHY xb32drthcyfe should fail and p@ssword1 should succeed.我们在这里遇到的问题是我们不知道为什么xb32drthcyfe应该失败而p@ssword1应该成功。 We can guess, but our scenario has failed to record the WHY.我们可以猜测,但我们的场景未能记录 WHY。

Now consider现在考虑

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

Now you have a single scenario that not only documents the WHY, but allows you to challenge the WHY, and explore the WHY eg现在,您有一个场景,不仅可以记录 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.

Now you can present the above behaviour to your business and say, do we really want to do this.现在您可以向您的企业展示上述行为并说,我们真的想这样做吗?

Every example should have a unique named rule behind it.每个示例背后都应该有一个唯一的命名规则。 For an example to become mature enough to reside in your cukes you need to reveal this rule, give it a name and then replace the usage of the example in your cukes with scenarios exploring the rule.为了让示例变得足够成熟以驻留在您的 cukes 中,您需要揭示此规则,为其命名,然后将示例在您的 cukes 中的用法替换为探索规则的场景。

You have to use the column headers in your step defs as params so cucumber can replace the params with values in your table eg您必须在步骤 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM