SpecFlow / Gherkin - возможно ли использовать одни и те же примеры в нескольких «измерениях»? - PullRequest
0 голосов
/ 31 января 2020

Вот простой набросок сценария:

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              |

... и каждый сценарий выполняется «мультипликативно» для каждого значения «кнопки».

Есть ли хороший способ сделать это?

1 Ответ

2 голосов
/ 01 февраля 2020

Вот один из возможных вариантов.

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 разделите expectedResults и buttons на ,, а затем сравните длину массива. Как только длина массива совпадет, получите элемент на основе индекса из обоих массивов и выполните одну из следующих 2 опций.

Опция 1: Реализуйте лог c непосредственно в шаге def.

Вариант 2: вызвать шаги When I press plus и Then the result should be <expectedResult> on the screen с помощью шага.

...