Data Provider
Sometimes parameters in testng.xml might not be sufficient, if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In such case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with @DataProvider
A @Test method specifies its Data Provider with the dataProvider attribute. This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.
Lets create Java class, passing a String Parameters
DataProvider name is “logindata” as name we should use in @Test method
package testngexp;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
publicclass dataprovideexample {
@Test(dataProvider = "logindata")
publicvoid test(String username, String pwd) {
System.out.println("UserName-->"+username);
System.out.println("Password-->"+pwd);
}
@DataProvider(name = "logindata")
public Object[][] provideData() {
returnnew Object[][] {
{ "nikil", "12345" },
{ "Raj", "rere" },
{ "Kanti", "Kanti" }
};
}
}
TestNG.xml file
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<parametername="MethodName1"value="SuiteTagTestMethod1"/>
<testname="Test1"preserve-order="true">
<classes>
<classname="testngexp.dataprovideexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
Results
Comments
Post a Comment