TestNG Tutorials 58: DataProvider in TestNG – Running Test for Specific Set of Data Provided by DataProvider Method – Dataprovider With Indices

By default a Test method is executed for all data set returned by DataProvider. The reason behind this is that a DataProvider annotated method has an attribute called “indices” whose default value is set to “all”.

You may need to run only specific set of data. Like if DataProvider providing you five set of data and you want to run your linked test method only for data set say 3rd and 4th, you can use indices concept. Index starts from zero and if data set is not present for mentioned index, no error or exception will be thrown.

Remember Indices are provided in DataProvider annotated method not Test annotated method.

We will see an example below:

package DataProvider;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderWithIndices {
	// You must need to mention data provider method name in Test method
    public void studentRegistration(String First_name, String Last_Name, String Email_ID) {
        System.out.println("Registered student with details: "+First_name+" "+Last_Name+" "+Email_ID);    
	// A data provider method with return type as 2D array
	// Data provider indices starts from zero.
    @DataProvider(name="DataContainer" , indices = {2,4})
    public Object[] myDataProvider() {
    	Object data[][]=  new Object[5][3];
    	// First student details
    	data[0][0]= "Mukesh"; 
    	data[0][1]= "Otwani";
    	data[0][2]= "Motwani@gmail.com";
    	// Second student details
    	data[1][0]= "Amod";
    	data[1][1]= "Mahajan";
    	data[1][2]= "amahajan@hotmail.com";
    	// Third student details
    	data[2][0]= "Animesh";
    	data[2][1]= "Prashant";
    	data[2][2]= "aprashant@gmail.com";
    	// Fourth student details
    	data[3][0]= "Ankur";
    	data[3][1]= "Singh";
    	data[3][2]= "asingh@gmail.com";
    	// Fifth student details
    	data[4][0]= "Amritansh";
    	data[4][1]= "Kumar";
    	data[4][2]= "akumar@gmail.com";
        return data;


[RemoteTestNG] detected TestNG version 6.14.2
Registered student with details: Animesh Prashant aprashant@gmail.com
Registered student with details: Amritansh Kumar akumar@gmail.com
PASSED: studentRegistration("Animesh", "Prashant", "aprashant@gmail.com")
PASSED: studentRegistration("Amritansh", "Kumar", "akumar@gmail.com")

Default test
Tests run: 2, Failures: 0, Skips: 0

Default suite
Total tests run: 2, Failures: 0, Skips: 0


This feature is not more helpful as you need to go and edit in DataProvider method if you want to update indices.

This feature would have been more useful if :
1. Indices are allowed to provide in Test annotated methods instead of DataProvider annotated method. It would help in maintaining less DataProvider methods for multiple Test methods.
2. Indices are allowed to pass from testng xml.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

3 thoughts on “TestNG Tutorials 58: DataProvider in TestNG – Running Test for Specific Set of Data Provided by DataProvider Method – Dataprovider With Indices

Leave a Reply

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