Data Provider with Iterator


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.The only difference with Object[][] is that an Iterator is create your test data lazily.

Lets create a java class

package testngexp;
import java.util.ArrayList;
importjava.util.HashSet;
import java.util.Iterator;
importjava.util.Set;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

publicclass iteratorexample {

@DataProvider(name="ItrData")

public Iterator<Object[]> logdata(){
ArrayList<Object[]> s=new ArrayList<Object[]>();
s.add(new Object[]{"Kiran"});
s.add(new Object[]{"Amul"});
s.add(new Object[]{"Vardhan"});
return s.iterator();
}

@Test(dataProvider="ItrData")
publicvoid Testmethod(String username){
System.out.println("User name"+username);
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<testname="Test1">
<classes>
<classname="testngexp.iteratorexample"/>
</classes>
</test><!-- Test -->
</suite><!—Suite -->

Results

image

Comments