Cucumber is a popular testing framework used for behavior-driven development (BDD). It allows developers to write tests in a natural language style, making it easier to collaborate with non-technical team members.
Data Driven Testing which allows to automatically run a test case multiple times with different input and validation values.
This is very often required in any automated test to pass data or to use the same test again with different data set. And the good part is that the Cucumber inherently supports Data Driven Testing using Scenario Outline. There are different ways to use the data insertion within the Cucumber and outside the Cucumber with external files.
Data-Driven Testing in Cucumber
Parameterization without Example Keyword
Data-Driven Testing in Cucumber using Scenario Outline
Parameterization with Example Keyword
Parameterization using Tables
Data-Driven Testing in Cucumber using External Files
Parameterization using Excel Files
Parameterization using Json
Parameterization using XML
In this blog i will explain how i implement data-driven testing in Cucumber using a Scenario Outline with Examples Keyword.
Lets understand what is Scenario, Scenario Outline and example .
What is a Scenario in Cucumber?
A scenario is a sequence of steps that represents a behavior of the application as expected by the user. Scenarios are written using the "Scenario" keyword followed by a descriptive title. Steps for the test case are provided using Given, When, and Then steps (or And/But for additional steps). The Scenario is not parameterized, meaning it uses fixed values for input and checks against predefined expected outcomes.
What is a Scenario Outline?
Scenario Outline is used when the same test is performed multiple times with a different combination of values. It uses the "Scenario Outline" keyword and includes placeholders (usually enclosed in angled brackets "<>") in the steps where data varies. The actual data sets are provided in a data table using the "Examples" keyword, and each row of the data table represents a separate test instance. Scenario Outlines are particularly useful for data-driven testing, allowing you to run the same scenario with different inputs to ensure the application behaves correctly under various conditions.
Steps to Use Examples and Scenario Outline
1.Define the Scenario Outline
In your feature file, use the Scenario Outline: keyword to define a scenario that will be executed with different sets of data. Replace the specific values with placeholders, indicated by angle brackets.
2. Add the Examples Table
Below the Scenario Outline, add an Examples: section with a table. Each row in the table represents a set of data for the scenario. Replace the placeholders in the scenario steps with the corresponding values from the table.
3. Execute the Scenario
During the execution of your feature file, Cucumber will execute the scenario for each row in the Examples table, substituting the placeholders with the data from each row.
Find below the snippet of the application i am going to test.
We can write separate methods for each parameter that we want to test, but that will make tests hard to maintain with many repetitions. Instead, we can use a Scenario Outline to add different inputs or arguments to the same scenario. We can write it like this:
When comparing a regular Scenario Definition with Scenario Outline, values no longer need to be hard-coded in step definitions. Values are replaced with parameters as <parameter_name> in step-definition itself.
At the end of Scenario Outline, values are defined in a pipe-delimited table format using Examples.
The Example’s section is a table where each argument variable represents a column in the table, separated by “|”. Each line below the header represents an individual run of the test case with the respective data. As a result, if there are 4 lines below the header in the Examples table, the script will run 4 times with its respective data.
When Cucumber starts to run this program, first, it will map parameters from the data table to placeholders like, and soon in the Feature File. The corresponding StepDefinition of Feature file is mentioned above. The steps can use delimited parameters that reference headers in the examples table. The cucumber will replace these parameters with values from the table before it tries to match the step against a step definition. There is no change in the StepDefinition as mentioned in the above example.
Using Tags in Examples
One another feature which i find useful for different types of testing are using Tags in the examples.
Scenario Outline:Queue page validation link land in correct page
Given The user is in the Queue page after logged in
When The user clicks <link name> Link
Then The user should be directed to <link name> page
@feature
Examples:
|link name|
|"Implementation of Queue in Python"|
|"Implementation using collections.deque"|
|"Implementation using array"|
|"Queue Operations"|
@regression
Examples:
|link name|
|"Implementation of Queue in Python"|
|"Implementation using collections.deque"|
The execution will be done based on the tags for Feature testing all four examples data will be executed but for regression only two examples data will be executed.
The Best Practice for Scenario Outline are
Keeping templates simple and easy to understand.
Organize your example data in a logical and easy to read format.
Troubleshooting Scenario Outlines
Debugging Common Issues
Make sure to use the correct syntax for Scenario Outlines.
Ensure that the placeholders match the data in the Examples section.
Check for typos and formatting issues.
Conclusion
Scenario Outlines are a powerful feature in Cucumber that enable you to write parameterized tests. By following best practices and using Scenario Outlines effectively, you can reduce code duplication and make your tests more efficient.In My case i reduced my lines of code 75% following this approach. For repeated actions with different combinations of data, Scenario Outline-Examples is a better choice as it does not require separate code to interpret the test data, Cucumber automatically interprets it. However, if we don't want to repeat the entire scenario but just one step, Data Table can be used.
Utilizing these rich features of Cucumber enables Data Driven Testing and Parameterization, allowing engineers to create robust and reusable test scenarios, enhanced test coverage, reduce redundancy and make our test steps more readable and understandable.
Hope you find this information useful.