top of page

Data Driven Testing in Cucumber

What Is Data-Driven Testing?

The data is what drives the test. Data-driven testing is most useful for tests that have a similar structure but use different data. You can take the same test case and run it with as many different inputs as you like, which increases the coverage from a single test. Data driven testing means passing data with the help of external sources like xls,xlsx,csv files., etc. We can also combine positive and negative scenarios into a single test case.

We can take the login page as a simple example for data driven testing. If you want to test login page with multiple users .Rather than create a different test for each login, you create one test.Then you use a data source to set the test data and validation criteria for each login. This has the key advantage of separating the test logic from the test data. Also, if one can easily modify or increase the number of test parameters by adding more username and password fields.

Why Data-Driven Testing

  • Test maintenance is reduced with Data driven testing, you only have one test case to maintain rather than duplicate test cases that each examine a specific set of data values.

  • Change the test data to easily add or delete situations without modifying the test process.

  • It increases Reusability and also it is easy to maintain.

  • Data-driven testing also reduces the risk of unnecessary duplicates and redundancy of test cases and automated test scripts.

  • We can use a single test case to cover all positive and negative cases.

  • It improves test coverage and also helps in faster execution.

Data driven testing in Selenium cucumber

In cucumber you can pass data from cucumber feature files to your test cases to achieve data driven testing .Let us consider the same example of login that you are automating the login flow of some application. In this case, you can pass username and password as test data from your feature file to selenium code.

There are different ways to use the data insertion within the Cucumber and outside the Cucumber with external files. Below are two ways to do DDT in cucumber.

  1. Using Scenario Outline and Example

  2. Parameterization without Example Keyword

How to achieve Data driven testing in Selenium cucumber

We have learned why we need data driven testing. Now we will learn how to do it in cucumber. As mentioned to avoid hardcoding , we can use feature file to pass data in cucumber. Let's see in detail how to achieve this.

Data-Driven Testing in Cucumber using Scenario Outline and Example

Scenario Outline - This is used to run the same scenario for 2 or more different sets of test data. E.g. In our scenario, if you want to login another user you can data drive the same scenario twice.

Examples - All scenario outlines have to be followed with the Examples section. This contains the data that has to be passed on to the scenario.

Below is feature file of ‘Login’.

As you can see in the above example , we have to​​ use the ‘Examples’ keyword with ‘Scenario Outline’ to pass multiple sets of data in our test script, these 2 go hand-in-hand. You​​ cannot​​ use ‘Examples’ keyword with normal ‘Scenario’.​​

In Scenario Outline we have given <username> and <Password> and same <username> and <Password> we have used in ‘Examples’ as column names. Here ‘Examples’ will act as a test data table separated by “|”.We can give multiple data to test our application. So the entire scenario will be executed 2 times since we have 2 rows in our ‘Examples’​​ table.

We have to use ‘Regular Expression’ in Test Steps of step Definition to make it understand the Parameterization of the feature file. With the help of the below statements, Cucumber will understand that the associated Test_Step is expecting some parameters.

@And("^user enters \"(.*)\" and \"(.*)\"$")

Step Definition of ‘Login’

In above example String username and String Password will take data from feature file and test scenario will get executed twice.

Parameterization without Example Keyword

If we want to pass a single set of data for our scenario, instead of hardcoding values in Test Step we will pass data from our feature file.

Login Feature file without Examples keyword

In the above example , We have not used Scenario outline and Examples Keyword as we are passing only a single set of data.

And user enters "xyz1234" and "kk12kk12"

In the above statement, we have passed Username & Password from the Feature File which will feed in to Step Definition of the above statement automatically. In Step Definition here also we have to use ‘Regular Expressions’

@And("^user enters \"(.*)\" and \"(.*)\"$")

@And("^User enters "([^"])" and "([^"])"$")

Step definition corresponding to the above feature file

In above example String username and String Password will take data from feature file and test scenario will get executed only once with single data.


As data driven testing itself means testing which is driven by data. It improves readability of scenarios and helps in maintenance of test scripts. We can achieve an excellent test-coverage and can combine positive and negative scenarios , this helps in achieving high quality and better stability of the application.

We are not hard coding the test data in the script logic, this will reduce the size of the code written. The same test script can be used for a huge set of test data. We can run the same Scenario multiple times, with different combinations of values. It is usually used for regression testing or with test cases where the same test scripts are run by providing huge test data as input.

In current time data is everything, the requirement of automating the process of data-driven testing arose and today we have several automation-testing tools and automation testing frameworks for this purpose.

Happy Learning…

3,089 views0 comments


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page