DataProvider is one of the best features of TestNG framework. We can use TestNG DataProvider to inject arguments into our test methods.

TestNG DataProvider

TestNG DataProvider helps us in creating loosely coupled test cases. We can separate testing logic and input data into different methods, this way we can also reuse the input data from a DataProvider for multiple test methods.

Let’s see how we can create a simple DataProvider for a TestNG class using Eclipse plugin. In Eclipse, go to New | Other | TestNG Class and in the wizard screen, select @DataProvider annotation.

When you click on Finish button, we get a default implementation of TestNG test class with DataProvider method.

Note that it’s not necessary to use Eclipse TestNG plugin, but it helps us in easily creating a base class and then change it according to our project use case.

Notice that dp() method is annotated with @DataProvider annotation and our we have used it in @Test configuration.

DataProvider method must return Object array and its dimension must match to the test method. Since our test method has two arguments, our DataProvider method must return Object[][] so that it gets mapped to the method arguments.

Just run the above class as TestNG test and we will get following output in the console.

TestNG @DataProvider Properties

@DataProvider annotation has three properties:

  1. name: This is used to specify the data provider method name, default value is the function name. We can use it to give a proper name to our Data Provider method.
  2. indices: This is a very important property, this allows us to specify the indexes we want to use for our testing purpose. This is helpful when our data provider method may have a large number of values and we want to run our test cases for only a few of them. Its default value is “all” so tests will run for all the inputs generated by the data provider method.
  3. parallel: This parameter tells TestNG to run tests in parallel. So the input to test methods can be injected in random order. By default inputs are injected in the order and tests are executed sequentially.

Let’s see an example where I have changed the name of DataProvider method and also using only a few of the inputs generated by the method.

Now when we run our test class, it generates following output.

Now change the DataProvider method to run in parallel.

The output generated when test class is executed:

Note that indices values don’t cause any exceptions if the DataProvider is returning fewer numbers of objects than specified. So below DataProvider implementation will work fine too.

TestNG DataProvider Class

TestNG is designed to be loosely coupled, so it’s no wonder that we have the option to separate our test class with data provider class. We can use @Test property dataProviderClass to provide the name of the class to look for DataProvider method.

If this attribute is specified, the data provider method needs to be static in the specified class. If not specified, the data provider will be looked in the test class or its superclasses.

Here is an example of separate TestNG DataProvider class.

Our updated Test class:

TestNG DataProvider with Factory

We can use DataProvider with Factory methods too. Below code snippet shows a simple example for using DataProvider with Factory annotation.

I have explained the use case and the benefits of using DataProvider with Factory in detail at TestNG Factory annotation tutorial.


TestNG is an awesome testing framework. DataProvider is one of the best features it has that I totally love and use a lot in my test cases.

By admin

Leave a Reply

%d bloggers like this: