SpecFlow / Gherkin-是否可以在多个“维度”中使用相同的示例?

问题描述 投票:0回答:1

这是一个简单的方案大纲:

Scenario Outline: Add two numbers
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press <button>
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | button | expectedResult |
    | 1           | 2            | plus   | 3              |
    | 2           | 2            | plus   | 4              |
    | 4           | 5            | plus   | 9              |

我想在多组其他条件下运行所有​​这些测试数据。我可以这样:

Scenario Outline: Add two numbers
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press <button>
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | button | expectedResult |
    | 1           | 2            | plus   | 3              |
    | 2           | 2            | plus   | 4              |
    | 4           | 5            | plus   | 9              |
    | 1           | 2            | add    | 3              |
    | 2           | 2            | add    | 4              |
    | 4           | 5            | add    | 9              |
    | 1           | 2            | +      | 3              |
    | 2           | 2            | +      | 4              |
    | 4           | 5            | +      | 9              |

...或类似这样:

Scenario Outline: Add two numbers (plus)
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press plus
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | expectedResult |
    | 1           | 2            | 3              |
    | 2           | 2            | 4              |
    | 4           | 5            | 9              |

Scenario Outline: Add two numbers (+)
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press +
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | expectedResult |
    | 1           | 2            | 3              |
    | 2           | 2            | 4              |
    | 4           | 5            | 9              |

Scenario Outline: Add two numbers (add)
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press add
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | expectedResult |
    | 1           | 2            | 3              |
    | 2           | 2            | 4              |
    | 4           | 5            | 9              |

...但是两者都涉及很多重复。我真正想写的是这样的:

Scenario Outline: Add two numbers
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    When I press
        | button |
        | plus   |
        | +      |
        | add    |
    Then the result should be <expectedResult> on the screen
Scenarios: 
    | firstNumber | secondNumber | expectedResult |
    | 1           | 2            | 3              |
    | 2           | 2            | 4              |
    | 4           | 5            | 9              |

...并使每个方案针对“按钮”的每个值“乘法”执行。

是否有很好的方法?

cucumber bdd specflow gherkin
1个回答
0
投票

这里是可能的选项之一。

Scenario Outline: Add two numbers
    Given I have entered <firstNumber> into the calculator
    And I have entered <secondNumber> into the calculator
    Then validate the <expectedResults> by clicking <buttons> button

    Scenarios:
      | firstNumber | secondNumber | buttons               | expectedResults |
      | 1           | 2            | plus,add,+,multiply,x | 3,3,3,2,2      |
      | 2           | 2            | plus,add,+,multiply,x | 4,4,4,4,4      |
      | 4           | 5            | plus,add,+,multiply,x | 9,9,9,20,20    |

在步骤def中将expectedResultsbuttons,分开,然后比较数组长度。一旦数组长度匹配,则从两个数组中获取基于索引的项目,然后执行以下2个选项之一。

选项1:直接在步骤def中实现逻辑。选项2:在步骤中使用When I press plusThen the result should be <expectedResult> on the screen步骤调用步骤。

© www.soinside.com 2019 - 2024. All rights reserved.