More with cucumber.

Sometime back we wrote about getting started with cucumber. Hope you liked it. In this post I want to share you some tips which will make your cases more elegant and managable.

DRYing your test cases.

When you start using cucumber exhaustively,  you will notice at one point that you end up writing the same steps again and again. And gradually  when your feature file grows bigger and bigger it becomes very tough manage.

  Feature: Signin
    As a registered user
    I want to signin with my details
    So that I can access my dashboard

  Scenario: Successful sign in
    Given I am a registered user and my email is "deepak@example.com" and my password is "password"
    When I am on the "signin" page
    And I fill in "Email" with "deepak@example.com"
    And I fill in "Password" with "password"
    And I press "Sign In"
    Then I should be on the "dashboard" page
    And I should see "Signed in successfully."

  Scenario: Checking unread messages
    Given I am a registered user and my email is "deepak@example.com" and my password is "password"
    When I am on the "signin" page
    And I fill in "Email" with "deepak@example.com"
    And I fill in "Password" with "password"
    And I press "Sign In"
    Then I should be on the "dashboard" page
    And I should see "Signed in successfully."
    When I have "2" unread messages
    Then I should see "you have 2 unread messages."

  Scenario: Checking the recent activites
    Given I am a registered user and my email is "deepak@example.com" and my password is "password"
    When I am on the "signin" page
    And I fill in "Email" with "deepak@example.com"
    And I fill in "Password" with "password"
    And I press "Sign In"
    Then I should be on the "dashboard" page
    And I should see "Signed in successfully."
    When I have some recent activities
    Then I should see those recent activities in the dashboard

If you look at the above example, in every scenario, 7 steps are repeated again and again. The ideal solution is to DRY them up. Cucumber allows you accomplish this by setting up a background.

  Feature: Signin
    As a registered user
    I want to signin with my details
    So that I can access my dashboard

  Background:
    Given I am a registered user and my email is "deepak@example.com" and my password is "password"
    When I am on the "signin" page
    And I fill in "Email" with "deepak@example.com"
    And I fill in "Password" with "password"
    And I press "Sign In"
    Then I should be on the "dashboard" page
    And I should see "Signed in successfully."

  Scenario: Checking unread messages
    When I have "2" unread messages
    Then I should see "you have 2 unread messages."

  Scenario: Checking the recent activites
    When I have some recent activities
    Then I should see those recent activities in the dashboard

This way of writing the repetitive steps in the background is best a practice to follow. It is worth knowing that the background is executed before the every scenario is executed. Which means the order of execution will be,

Background -> Scenario1,
Background -> Scenario2,
Background -> Scenario3.

Sadly you can only set up a single background for every feature. It would be really interesting if cucumber had allowed backgrounds with a context. Which means we could have some thing like,

Background1 -> Scenario1
Background1 -> Scenario3
Background1 -> Scenario5

Background2 -> Scenario2
Background3 -> Scenario4

The only way we can accomplish the above is to break them into multiple features.

#feature1
Background1 -> Scenario1
Background1 -> Scenario3
Background1 -> Scenario5

#feature2
Background2 -> Scenario2
Background3 -> Scenario4

DRYing up further more
If you are lazy enough to break it down into multiple features, then there is some good news for you. Yes, cucumber allows you to group a set of steps into a single step, and this can be done in your step definition.

  Scenario: Successful sign in
    Given 'I "deepak@example.com" have an invitation for the role "Admin"'
    Then 'I should receive an email'
    When 'I open the email'
    Then 'I should see "Invite to join example.com" in the email subject'
    And 'I should see "Click here to sign up" in the email text body part'

You can group all of the above steps into one.

  #In your step definition
  Given /^I have opened my invitation email and clicked the sign up link$/ do |arg1|
    Given 'I "deepak@example.com" have an invitation for the role "Admin"'
    Then 'I should receive an email'
    When 'I open the email'
    Then 'I should see "Invite to join example.com" in the email subject'
    And 'I should see "Click here to sign up" in the email text body part'
  end

Once you have done the above, you can use it directly in your feature.

  Scenario: Successful sign in
    Given I have opened my invitation email and clicked the sign up link



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.