Is it possible to reuse a feature as the "Given" for another feature?

Michael Baldry picture Michael Baldry · May 30, 2012 · Viewed 13k times · Source

Is it possible to reuse a feature as the "Given" for another feature?

Or am I trying to do something I shouldn't be trying to do

basically my features look like:

Scenario: Creating a basic account with valid details (happy path)
  Given I am on the "signup" page
  And I enter all the right details #this is shortened of about 20 steps for your reading ease
  When I press the button labelled "Sign up"
  Then I should see the text "Thanks for signing up"
  And I should have an email from "[email protected]" titled "Confirm your account Michael"
  And the database should contain a record for the user marked as unconfirmed

Scenario: Confirming account via email
  Given I have created a basic account
  When I open the confirmation email and visit the url to confirm the account
  Then I should be logged in
  And the database should contain a record for the user makred as confirmed

I clear my DB after every feature as they should all be able to be run individually...

Am I going about this the wrong way?

Thanks

Answer

Todd A. Jacobs picture Todd A. Jacobs · May 30, 2012

The Problem

What you're actually attempting is to reuse a scenario. This is no longer supported in Cucumber.

Aside from other problems with this approach, your tests will be slower and interdependent, since you will be:

  1. driving account creation through a browser, and
  2. making all your tests dependent on the account-creation test passing.

Don't do that.

The Cucumber Way

Generally, you should write your tests to work independently, although you can certainly reuse step definitions. So, in the general case, you might want to add shared steps like:

  1. Given that user account "Test User" does not exist
  2. Given that user account "Test User" exists

which can then be included in your scenarios as needed. The nice thing about this approach is that the steps can create or delete users programmatically.

Alternatively, if most of your tests will be on existing accounts, set up the default data set with the right user fixtures already in place. For the limited subset where you will be testing account creation, just add a scenario background that drives user deletion.