How to call one scenario from another? #GivenWhenThenWithStyle

The next Given-When-Then with style challenge is to remove duplication from similar scenarios, in particular when groups of steps are shared between different scenarios.

How can we call one scenario from another? We have several large feature files with a lot of duplication, and some smaller scenarios that contain blocks of steps from the large ones. We’d like to use smaller scenarios as components for the big ones.

A typical example of this problem is describing workflows, when different workflow branches may require same or very similar paths to test. Here is a sample involving user registration functions:

Feature:

Scenario: User registers

Given a visitor opens the homepage
And the visitor enters a new username into the registration form
And the visitor enters a valid password into the registration form
When the visitor submits the registration form
Then the registration should be successful
And a new user account should be created
And the visitor is be redirected to the login form

Scenario: User signs in after registration

Given a visitor opens the homepage
And the visitor enters a new username into the registration form
And the visitor enters a valid password into the registration form
When the visitor submits the registration form
And the visitor is redirected to the login form
And the visitor enters the username and password into the sign in form
When the visitor submits the sign in form
Then the signed in user page should be shown
And the visitor username should be displayed as the account name

Scenario: User requests password reset

Given a visitor opens the homepage
And the visitor enters a new username into the registration form
And the visitor enters a valid password into the registration form
When the visitor submits the registration form
And the visitor is redirected to the login form
And the visitor clicks the forgot password link in the sign in form
Then the visitor is redirected to the forgot password form
When the visitor enters his registered email
And the visitor submits the forgot password form
Then the password reset notification should be sent
And the visitor should be redirected to the password reset completion form

Scenario: User completes password reset
...

A trivial solution for this case would be to create a common background. But to further complicate the challenge, let’s add to this cases where the shared steps are not just at the top of each scenario, but could be in the middle or at the end as well.

Feature: new account purchases

Scenario: User tops-up balance immediately after registration

Given a visitor opens the homepage
And the visitor enters a new username into the registration form
And the visitor enters a valid password into the registration form
When the visitor submits the registration form
And the visitor is redirected to the login form
And the visitor enters the username and password into the sign in form
When the visitor submits the sign in form
And the signed in user page is shown
And the user opens the balance page
And the user adds credit card information on the payment form
And the user selects 10 EUR as the top-up amount
And the user submits the payment form
And the payment completes
Then the user balance should show 10 EUR
And the user clicks the log-out button
Then the user is logged out

Scenario: User tops-up balance after re-login after registration

Given a visitor opens the homepage
And the visitor enters a new username into the registration form
And the visitor enters a valid password into the registration form
When the visitor submits the registration form
And the visitor is redirected to the login form
And the visitor enters the username and password into the sign in form
When the visitor submits the sign in form
And the signed in user page is shown
And the user clicks the log-out button
Then the user is logged out
And the visitor enters the username and password into the sign in form
When the visitor submits the sign in form
And the signed in user page is shown
And the user opens the balance page
And the user adds credit card information on the payment form
And the user selects 10 EUR as the top-up amount
And the user submits the payment form
And the payment completes
Then the user balance should show 10 EUR
And the user clicks the log-out button
Then the user should be logged out

As the larger scripts get more complicated, they become more difficult to manage, and the duplication starts to hurt a lot. Reusing parts of scenarios (or entire scenarios) is a common feature of test management tools, and people coming to Given-When-Then specs from a testing background often look for a way to call one scenario from another, or reuse flows.

How would you rewrite these scenarios to avoid duplication?

Stay up to date with all the tips and tricks and follow SpecFlow on Twitter or LinkedIn.

PS: … and don’t forget to share the challenge with your friends and team members by clicking on one of the social icons below 👇