Make Selenium Easy

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:

Output:

[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.
#ThanksForReading
#HappySelenium

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.