How Can We Use DataProvider In TestNG? Explained With Examples

Parameterization in TestNG assists us in passing data through code and payment hardcoding. Nevertheless, test parameters empower us for giving the values only once per execution cycle. For overcoming this, we could use a DataProvider in TestNG annotations order for passing multiple parameters to a single test in a single execution. Using DataProvider will be very easy for us to give various values in a trial for just one execution cycle.

Latest quickly understand more about DataProvider in TestNG and how to efficiently use them in our test scripts for selenium test automation.

What is DataProvider in TestNG?

Data provider’s interest entries are a way to pass parameters in the test function, the other being test and parameters. DataProvider always gives different values to the TestNG groups cases in a single execution. In the form of TestNG annotations order, it is a part of the inbuilt data-driven testing for which TestNG is very popular. DataProvider assists us in passing the parameter in different ways.

DataProvider Syntax:

The TestNG example DataProvider is used in the following manner:

@ DataProvider (name = “name_of_dataprovider”)

public Object[][] dpMethod() {

return new Object [][] { values}


Now let us understand the different components of the syntax.

  1. The DataProvider annotation has a unique attribute called name, which we can select as per our convenience.
  2. DataProvider is different in systems used in various test functions, which indicates that this annotation is not practiced on test functions like the TestNG groups parameter.
  3. The data provider system returns a 2D list of objects.
  4. In case we do not define a name for the data provider, the data provider method is considered its default name. So the name of the data provider describes the data provider method.

TestNG examples present additional information about the class or method we described above. It is normally represented by the ‘@’ prefix. These annotations are used by TestNG to help in developing a robust framework.

How to use DataProvider in TestNG?

If we have read the above-said points using DataProvider is going to be very easy for us. We will start with a simple and basic data provider test first. Observe the following code, which contains the @Dataprovider.

DataProvider In TestNg

In the above code, we are trying to pass the values “first value” and “second value” for the test method “my test” using the guidance of data provider method “method().” Refer to the syntax section for remembering the points once again.

dataprovider in testng

Both the values arrived in the output, which means that even though we run the file once, the test case procedure ran twice with distinct values. Even though it is a very small and simple code, we might know how much the codes could mess up if we have while testing. It wouldn’t be better to declare the test and the data provider in another class and test case into another. It is inheriting the DataProvider.

Inherited data provider in TestNG

Data provider and the test case procedure could also be used in two separate classes. As it is inheriting the data provider since we are inheriting it from another file. It is expected to slightly change the above code for running the test case like this. Below provided is a @Test file :

dataprovider in testng

The only difference here is along with the data provider’s name that we are now required to implement the data provider class using the same attribute name.

Since we used this method for our data provider class in, we will create another file and write our data provider code. The looks like this:

dataprovider in testng

Now we will run the test file and see if the output is “Value Passed” or not.

dataprovider in testng

The output tells that the variable value “value passed” was passed by the method. In the above code, we have given a single parameter for the execution of the test case. Essentially we require much more. In the next section, we will see how to pass multiple parameters in the TestNG framework data provider.

How to pass multiple parameters in TestNG DataProvider?

Passing multiple parameters is a very similar process to single parameters, but we will accommodate various values in a single parameter. Let us observe the following test code, which checks if the sum of two integers is as expected or not.

dataprovider in testng

In the above code, we have passed three values a,b and result to check if the sum is equal to the result or not.

The output will come as follows:

dataprovider in testng

This course provides us with the switch case for checking the method’s name and returning parameters according to the method name. After running the above code, see how the output compares:

dataprovider in testng

A single accomplishment failed where the expectation was the sum of 5 and 7 to be 9 whose failure was obliged to happen. We can see how in 5 lines, we created a data provider for two different test methods. We should notice that for every e technique added to the provided data provide the code, we need to add two lines:

case “method”:

          return statement.

This could also happen in a single line as we are doing this alternative li of adding 7-8 lines typically. We can now analyze how efficient this method is.

DataProvider in TestNG using Excel

Using Excel for DataProvider in TestNG is one of the most beneficial ways of reading the data. By doing so, our job becomes remarkably easy when we deal with vast amounts of data. For doing so, we must understand some simple steps for achieving our target of using Excel as a data provider.

It was all about the topic of DataProvider in TestNG. If we get the codes applied in this tutorial, we need to tell how efficient DataProvider is for passing the parameters. On the other hand, It will be mistaken if we assume a better choice than parameters in the TestNG framework. Each of these concepts is used individually and depends totally upon the needs of the tester. So we should master both of them with different scenarios and different data sets.