Search code examples
phpbddcodeceptiongherkin

Gherkin - Include common steps in many scenarios (Codeception)


I'm using gherkin driven by codeception for BDD in my project. I would like to test if user given with a role is able to see menu entry points which are suitable for him.

At this moment I use this scenario:

Scenario: basic menu check
   User "foo" has role "basic_user"
   Given I am logged in as "foo" with password "Test123!"
   Then I should see "Project" menu point
   And I should see "Settings" menu point
   And I should see "Notifications" menu point
   And I should see "Messages" menu point
   And I should see "Logout" menu point
   Then I logout

I would like to reuse 3 steps many times:

   And I should see "Settings" menu point
   And I should see "Notifications" menu point
   And I should see "Messages" menu point

I do not want to copy&paste it every time I create new scenario. Instead I want to write it as... lets say include file (also in gherkin language), and use it in my scenarios:

Scenario: basic menu check
   ...
   Include "common_menu_check"
   ...

Is it possible? How I can do that?


Solution

  • In addition to Kyle's answer I'd rather prefer to rewrite scenarios like in your example using table as multiline argument in the following way:

    Scenario: basic menu check
       User "foo" has role "basic_user"
       Given I am logged in as "foo" with password "Test123!"
       Then I should see menu points:
       | Settings      |  
       | Notifications |  
       | Messages      |  
       | Logout        |  
       Then I logout
    

    It is less imperative and more human-friendly ("I should see" in every line is too boring) so it would be easier to discuss with customer.

    Note that here table used as multiline argument, not as Example. See about table as multiline argument for steps here: