
In this guide, you’ll learn how to use scenario outlines and examples to write more dynamic and data-driven tests. This allows you to test multiple cases with different inputs and expected outcomes efficiently.
What is a Scenario Outline?
A Scenario Outline is a template for a scenario that can be run multiple times with different sets of data. It is useful when you want to test the same scenario with different inputs and expected outcomes. This helps in reducing redundancy and improving the readability and maintainability of your test suite.Syntax of Scenario Outline
The syntax of a Scenario Outline in Gherkin is as follows:
Scenario Outline: Title of the scenario
Given some precondition
When some action is performed
Then some expected outcome should happen
Examples:
| parameter1 | parameter2 |
| value1 | value2 |
| value3 | value4 |
- Scenario Outline: This keyword establishes a scenario template that allows for the reuse of the same test steps with varying data inputs, streamlining the testing process.
- Examples: This section specifies the various sets of data that will be used to fill in the placeholders defined in the Scenario Outline, enabling comprehensive testing of multiple scenarios efficiently.
Using Examples in Scenario Outline
The Examples section contains a table where each row represents a different set of values to be used in the scenario. The values from the table are substituted into the scenario outline, and the scenario is run once for each row.Consider a scenario where we want to test the products filter functionality of the Kicks website with different filters and verifying the item counts. This involves simulating user actions on the website to ensure that the filters for products work as expected.
Feature: Products Page Filters & Stock Count
Scenario Outline: User clicks product filter, choose a gender option and verify item count
Given the user is on the products screen
When the user clicks "<filter>" filter option
And the user clicks the "<gender>" gender option
And the user clicks a "<size>" shoe
Then the first sneaker option is "<first_sneaker>"
And the result is now "<item_count>"
Examples:
| filter | gender | size | first_sneaker | item_count |
| Most popular | Male | 10.5 | Adidas Converse | 7 items |
| Lowest price | Female | 6 | New Balance Streetwear | 13 items |
| Highest price | Male | 9.5 | Jordan Orginal | 9 items |
| Newest | Female | 7 | Nike Air Max 360 | 14 items |
This feature focuses on verifying that the product filters on the Kicks website are working correctly. It includes tests for various filters such as Most popular
, Lowest price
, Highest price
, and Newest
to ensure they return the correct item count.
This step sets the initial condition that the user is on the products page of the website.
This action simulates the user clicking on a specific filter option, such as Most popular
or Lowest price
.
This action simulates the user selecting a gender filter, such as Male
or Female
.
This action simulates the user selecting a shoe size, such as 10.5
or 6
.
Conclusion
Gherkin Scenario Outlines are essential for enhancing clarity and efficiency in your behavior-driven development (BDD) process. They allow for reusable scenarios with varying inputs, simplifying test case creation and promoting collaboration among team members. In the next guide, you’ll learn about Implementing Step Definitions, where we’ll connect your Gherkin scenarios to executable code.