Search code examples
cucumbercucumber-jvmgherkincucumberjscucumber-java

Gherkin table - remove repetition


I have a cucumber gherkin feature file shown below:

Feature: Log in

  Scenario Outline: Successful log-in
     Given i enter <username>
     Given and password <password>
     Then I log in

     Examples:
       | username | password |
       | hello | sorry |
       | hello | hello |
       | hello | goodbye |
       | admin | sorry |
       | admin | hello |
       | admin | goodbye |

as you can see above in the username and password table, there is a lot of repetition. how can i remove this repetition?

for example, i could create two features like

(1)

Feature: Log in

  Scenario Outline: Successful log-in
     Given i enter hello
     Given and password <password>
     Then I log in

     Examples:
       | password |
       | sorry |
       | hello |
       | goodbye |

(2)

Feature: Log in

  Scenario Outline: Successful log-in
     Given i enter admin
     Given and password <password>
     Then I log in

     Examples:
       | password |
       | sorry |
       | hello |
       | goodbye |

but there is still repetion in here.

is there some other way to remove this repetion. i would like something like:

Feature: Log in

  Scenario Outline: Successful log-in
     Given i enter <username>
       | hello |
       | admin |
     Given and password <password>
       | sorry |
       | hello |
       | goodbye |
     Then I log in

But i am not sure if the above kinda thing is possible...

PLease help me...

I have ommitted step definitions here since they are easy to do.


Solution

  • In short, no. There isn't a way to multiply out the examples in the Examples table.

    However, there is an alternative that aids readability and increases the understanding of the tests from the business (which is what you really want to do with BDD-style tests).

    Background:
      Given I am on the login page
    
    Scenario Outline: Logging in with valid passwords
      When I attempt to log in as <user_type> with a valid password
      Then I should see the <page> page
    
     Examples:
        | user_type | page            |
        | a user    | home            |
        | an admin  | admin dashboard |
    
    Scenario Outline: Logging in with invalid passwords
      When I attempt to log in as <user_type> with an invalid password
      Then I should see the password validation
    
     Examples:
       | user_type |
       | a user    |
       | an admin  |
    

    Background's can take away the repetition of the set up steps (assuming they are the same throughout all the scenarios within a feature) and if you group each scenario outline by what it is attempting to achieve, you should have more readability overall, with the intent of the tests clearly expressed.