Data Provider from Different classes
By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute
Here, we define data provider in dataproviderexp1 java class
package testngexp;
import org.testng.annotations.DataProvider;
publicclass dataproviderexp1 {
@DataProvider(name="dpdiffclass")
publicstatic Object[][] diffclassmethod() {
returnnew Object[][] {
{"kiran",20},
{"daya",40},
{"raga",50}
};
}
}
Create another class file
package testngexp;
import org.testng.annotations.Test;
publicclass dataproviderexp2 {
@Test(dataProvider = "dpdiffclass", dataProviderClass=dataproviderexp1.class)
publicvoid test(String username, intage) {
System.out.println("UserName-->"+username);
System.out.println("UserAge-->"+age);
}
}
TestNG.xml
<?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">
<classes>
<classname="testngexp.dataproviderexp2"/>
</classes>
</test><!-- Test -->
</suite><!—Suite -->
Results
Note: if your data provider is defined in different class (ex:dataproviderexp1) , it should be defined as Static like thispublicstatic Object[][] diffclassmethod() and you should specify the class name where the data provider is defined and in @Test method you should add dataProviderclass in another class(ex: dataproviderexp2) like this @Test(dataProvider = "dpdiffclass", dataProviderClass=dataproviderexp1.class)
Comments
Post a Comment