SpecFlow/Gherkin - is it possible to use same examples in multiple "dimensions"?

81 views Asked by At

Here's a simple scenario outline:

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              |

I'd like to run each of these sets of test data under multiple sets of other conditions. I can do like this:

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              |

...or like this:

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              |

...but both of those involve a lot of duplication. What I'd REALLY like to write is something like this:

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              |

...and have each Scenario execute "multiplicatively" against each of the values for "button".

Is there a good way to do this?

1

There are 1 answers

0
supputuri On

Here is one of the possible options.

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    |

In the step def split the expectedResults and buttons with , and then compare the array length. Once the array length match then get the item based on index from both the arrays and do one of the below 2 options.

Option 1: Implement the logic directly in the step def.

Option 2: call the steps When I press plus and Then the result should be <expectedResult> on the screen steps with in the step.