Search code examples
javascriptcucumberacceptance-testingcucumberjsscenarios

Use the same examples for multiple Cucumber js scenarios


I'm finding it difficult to use multiple Scenario Outlines to use the same example and run in a sequence. My use case is for a feature to test like this:

Scenario Outline: Time consuming login process
  When I enter login credentials for <user> 
  Then I should be on the home page

  Examples:
    | user |
    | user1 |
    | user2 |
    | user3 |

Scenario Outline: User action 1
  Given I am logged in
  When I do something
  Then I should see response

  Examples:
    | user |
    | user1 |
    | user2 |
    | user3 |

#Many more tests needed to be done for each user

The issue with using Scenario Outline for every Scenario is that the time consuming Scenario (login) would need to run for each test. To deal with this, I've run these tests as one big Scenario Outline:

Scenario Outline: 
  #Scenario: Time consuming login process
    When I enter login credentials for <user> 
    Then I should be on the home page

  #Scenario: User action 1
    Given I am logged in
    When I do something
    Then I should see response

  #More tests here

    Examples:
      | user |
      | user1 |
      | user2 |
      | user3 |

I've found running all tests as one large Scenario Outline has made them run faster, but the cucumber reports and distinction between the Scenarios is less clear.

Is there a way to nest Scenarios in Scenario Outlines? If not, what is the best practice for this situation?


Solution

  • Nesting creates dependent scenarios. It is a BDD anti-pattern to have dependent scenarios. Sequential scenarios are also an anti-pattern. Each scenario should be able to operate independently. So, that's not a solution. It might help to revisit your feature to better understand what behaviors support it.

    From what I understand, if you have a condition that applies to multiple scenarios of the same feature, it's considered a background scenario. So maybe if you need to check login behavior, do it once in the background. It will then, by default, apply to all that feature's scenarios.

    If you have state-dependent preconditions, use helper methods or a world constructor.

    If anyone has a more refined solution, please feel free to add.