Data Driven Testing in Cucumber BDD

Tutorial 10​​ ​​ ​​ Data​​ Driven Testing​​ in​​ Cucumber​​ BDD

What you will Learn :

  • Data Driven Testing using ‘Examples’ keyword plus​​ ‘Scenario Outline’

Data Driven Testing using ‘Examples’​​ keyword​​ plus​​ ‘Scenario Outline’

Let us create a​​ brand new​​ feature file​​ having ‘Login feature’.

If you are writing ‘Scenario Outline’, you​​ have to​​ use ‘Examples’ keyword, see below. These 2 go hand-in-hand. You​​ cannot​​ use ‘Examples’ keyword with normal ‘Scenario’ that we have been writing so far.​​ 

So,​​ if you really want to​​ use the concept of data driven testing, you​​ can achieve with the help of​​ ‘Examples’ keyword​​ and you have to use ‘Scenario Outline’.

Save the file.

The ‘Examples’ table acts like a​​ test​​ data table​​ for us. It contains multiple set of test data to test our application

Now, in the above ‘Scenario Outline’, look at line numbers 7 and 8. We have written “<Username>”​​ and​​ “<Password>”. So these are string values because these are written within double quotes​​ 

Now,​​ the <Username> that you see in the figure above is actually the column name that we have written in the ‘Examples’ table. Similar is the case with <Password>. These should be an exact match. You cannot write <Username> in line number 7 and USERNAME in ‘Examples’ table.​​ 

So the entire scenario will be executed 2 times since wehave 2 rows in our ‘Examples’​​ table

So, this is the concept of data driven testing in cucumber.​​ 

Let us run our feature file to generate our step definition methods

Copy the methods from console

Create a step definition file and paste the above methods

Import the methods and remove the exceptions

Save the file

Let us re-run our feature file

Notice the console output. The scenario got executed twice. The username/password was picked up from the ‘Examples’ table.​​ 

Also, 14 steps got passed since we have 7 steps in a scenario. This would be multiplied by 2 rows in the table, hence 14 steps​​ 

So, invalidusername/98765 was picked up in first scenario execution and way2automation/invalidpassword was picked up in second scenario execution​​ 

Let us now take another example​​ wherein both the rows have only numeric values, see below​​ 

Save the file and run it

Copy the step definition code

Create step def file and paste the code. Import the packages and remove the exceptions

Save the file

Re-run the feature file

Notice the console o/p. The scenario got executed twice as expected and 10 steps got passed

So this is how we use the ‘Scenario Outline’ and ‘Examples’ keyword combination​​ to execute​​ data driven testing​​ in cucumber.​​ 

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 1st Nov 2024