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:


[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.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning