Skip to main content

Using Cucumber Example Tables

The use of Cucumber example tables within your features and scenarios enables the repetition of tests with varying datasets. It also provides a very transparent means of demonstrating your varying test coverage. They can be used in a variety of different ways and some common examples can be found below.

Example 1. Inline Tables

Scenario

  Scenario Outline: Organisation created with specified end date for subscription id
Given I create a new organisation
| id | validity_end_date |
| <id> | <validity_end_date> |
When I specify a subscription expiry date and subscription id
Then the specified date and id overrides the default subscription type and its validity period expiration date
| id | validity_end_date |
| <id> | <validity_end_date> |

Examples:
| id | validity_end_date |
| EVALUATION | 2021-07-01T00:00:00+0000 |
| EVALUATION | 2021-05-01T00:00:00+0000 |

Matching Step Definitions

  Given(/^I create a new organisation$/) do |table|
table.hashes.each do |value|
@new_org = AdminOrg.new_org_with_validity_end_date(id: "#{value[:id]}", validity_end_date: "#{value[:validity_end_date]}")
end
end

Then(/^the specified date and id overrides the default subscription type and its validity period expiration date$/) do |table|
table.hashes.each do |value|
database.verify_subs_id_and_end_date('core', @new_org[:org_id], value[:id], value[:validity_end_date])
end
end

In the above example, we can see that the Given and Then step definitions both receive a 'table' parameter and the table and the table column header are then referenced in order to pass a specific value from the inline Cucumber scenario table to the Ruby method call within the associated step definintions.

Example 2. In Step References

Scenario

  Scenario Outline: Tab names are correct
Given a user is logged in to TE Reporting
When I navigate to the following url - <url>
Then the tab name is: <tab_name>
Examples:
| url | tab_name |
| / | Home |
| /dashboards/summary | All Dashboards |

Matching Step Definitions

  When(/^I navigate to the following url \- (.*)$/) do |url|
visit TE.environment['site_url'] + url
raise unless current_url == TE.environment['site_url'] + url
end

Then(/^the tab name is: (.*)$/) do |expected_tab_name|
raise "Tab name incorrect! \nExpected: #{expected_tab_name} \nActual: #{tab_title}" unless tab_title.include? expected_tab_name
end

In the above example, we can see that the When and Then step definitions each receive a unique parameter by way of reference to the underlying Cucumber scenario table. It is then directly referenced within the Ruby method call.

Example 3. In Step Strings

Scenario

  Scenario Outline: Can't create an organisation with invalid org ID
Given I am an authenticated user
When I make a POST request to api organisation with an orgId which is invalid as per the schema naming requirements
"""
<orgID>
"""
Then the response body contains a 'validation_error' error message
Examples:
| orgID |
| 1e2x |

Matching Step Definitions

  When(/^I make a POST request to api organisation with an orgId which is invalid as per the schema naming requirements$/) do |org_id|
new_org = AdminOrg.new_organisation(org_id: org_id)
@response = Admin.create_organisation(cookies: @cookies, payload: new_org, response_code: 400)
end

In the above example, we can see that the When step definition receives an 'org_id' parameter string by way of reference to the underlying Cucumber scenario table. It is then directly referenced within the Ruby method call.