Data-Driven Testing With TestNG đź’»

@DataProvider annotation

Fareena Imran
CodixLab
2 min readFeb 18, 2022

--

In this article, we’re going to learn about “how to perform Data-Driven Testing in TestNG framework.

What is Data-Driven Testing âť”

Data-Driven Testing is used when we need to test the same feature multiple times with a different set of input values.

How To Performâť”

Data-Driven testing performs with the help of @DataProvider annotation in TestNG. @DataProvider annotation parameterizes the specific test method when it is needed to run that test method multiple times with known data values.

◾ ️For Example, When we need to test login functionality multiple times with different credentials like username, email or passwords then we use @DataProvider annotation.

Explanation With Example

Let’s Consider the scenario and explain step by step with examples.

Scenario đź’­

Let’s say we need to make a test case to log in to the app with three different credentials i.e. user name 🙎 and password 🔑. Now follow the below 👇 steps to make a test case for this.

#1) @DataProvider Annotation

◾️ This annotation returns Java objects that store multiple values for the test method. It is used above the test method that has the test data. See the example below 👇

Use of DataProvider Annotation

In the above example, we stored the user's name 🙎 and password 🔑 in a multi-dimensional array.

The multidimensional array works on row and column, as we have 3 combinations and 2 arguments for each combination that's why it is [3][2].

As index value always start with 0 that’s why in the above example we start with 0 index.

#2) dataProvider Attribute

Now, we need to call these input values to the test method as arguments. So, for this, we use the “dataProvider” attribute above the test method that is assigned with the method name “loginData” that has input values as seen in the example 👇

Code for Practice

Output

Output

Follow my articles to learn more about the TestNG framework.

Best of Luck đź‘Ť

--

--

Fareena Imran
CodixLab
Writer for

SQA Engineer| Tech Writer | Write stories about SQA