bN_
bN_

Reputation: 884

Is it possible to add an "Else" after "Given, When, Then"?

I'm new to Gherking and trying to write my first scenarios as best as I can, but I regularly find myself in situations where I'm really tempted to add an "Else" to my scenario. "Given, When, Then" becomes "Given, When, Then, Else". I know that the "Else" keyword is not defined and so not implemented in Gherkin tools but it doesn't care to me because I don't use these tools.

Do you think it is correct to write this :

Example :

Scenario : Application starts
  Given I start the application
  When I already have an open session
  Then I see the home screen
  Else I see the login screen

Or is it better to write two different scenarios :

Scenario : Application started by authenticated user
  Given I have an open session
  When I start the application
  Then I see the home screen

Scenario : Application started by unauthenticated user
  Given I don't have an open session
  When I start the application
  Then I see the login screen

Upvotes: 0

Views: 2370

Answers (1)

SystemsInCode
SystemsInCode

Reputation: 699

In short no, but here are options to handle multiple variants of a scenario:

  • If it was only tailing elements of the scenario steps that differed you could of moved early steps in to a common 'Background' section, making repeated differing scenarios shorter and clearer.

But from your example it is all steps differing slightly so you can:-

  • accept the repitition of multiple scenarios

Or

  • parametise the differences and use data tables in the 'given' and 'then' sections to give before and after values.

Or (my prefernece)

  • Use the 'scenario outline' syntax that uses an examples table to provide sets of data fixtures with their expected results. These replace in the scenario steps as runtime. The scenario is then 'played out' once for each row in the examples table.

So:

    Scenario : Application started by authenticated user
    Given I have an open session
    When I start the application
    Then I see the home screen

    Scenario : Application started by unauthenticated user
    Given I don't have an open session
    When I start the application
    Then I see the login screen

Becomes:

    Scenario Outline: Application Start and login
    Given Application started by <AuthenticationStatus> user
    And I have <SessionState> session
    When I start the application
    Then I see the <FirstScreen> screen

    Examples:
    |AuthenticationStatus   |SessionState   |FirstScreen|
    |Authenticated          |open           |home       |
    |Un-Authenticated       |not open       |login      |

IMHO for 2 scenarios it might not be worth the loss of readabiltiy but for more than that I think it's definitely worth it.

Upvotes: 2

Related Questions