TestNG Tutorials 55: DataProvider in TestNG – Lazy Initialisation of DataProvider Method – Use of Iterator Return Type

Hello,

DataProvider is an important functionality provided by TestNG to achieve Data driven testing or providing a set of different dataset to a method to execute on. We have seen DataProvider concept in details in previous posts.

DataProvider may become nightmare if number of data (arguments) is more or it keeps changing or you do not know the required number of data in advance or number of parameters is dynamic. In all these cases, you can not define number of arguments for a DataProvider method and Test method easily. All these problem can be solved using Iterator to a List of data.

As per TestNG official document, “An Iterator lets you create your test data lazily. TestNG will invoke the iterator and then the test method with the parameters returned by this iterator one by one. This is particularly useful if you have a lot of parameter sets to pass to the method and you don’t want to create all of them upfront.”

In simple words, each iteration will be a set of data for a Test method to run on. We will see an example below:

A Student class:

A DataProvider method with Iterator as return type and its caller Test method:

A DataProvdider method will return an Iterator of a list containing Student objects. A caller Test method needs to accept type of Object returning by Iterator i.e. Student. See an example below:

Output:

[RemoteTestNG] detected TestNG version 6.14.2
You are eligible to do registration.
Student registered with details as Name = Kiran , Age =28 , Gender =Male
You are eligible to do registration.
Student registered with details as Name = Amod , Age =28 , Gender =Male
You are eligible to do registration.
Student registered with details as Name = Anu , Age =28 , Gender =Female
PASSED: Studentregistration(DataProvider.Student@544fe44c)
PASSED: Studentregistration(DataProvider.Student@18eed359)
PASSED: Studentregistration(DataProvider.Student@6c3708b3)

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

You can see Iterator can remove the dependency of defining arguments in advance. You can easily add/remove/modify members and logics without worrying about mismatch of arguments between DataProvider method and Test method.

More about TestNG in upcoming posts. Stay tuned.

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

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