@DataProvider with @Factory Method


Factories can also be used with data providers, and you can leverage this functionality by putting the @Factory annotation either on a regular method or on a constructor.

Lets create a java class

package testngexp;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

publicclassdpwithfactoryexp {
String msg;
@Factory(dataProvider="dpwithfactory")
public dpwithfactoryexp(String msgstr){
msg=msgstr;
}

@DataProvider(name="dpwithfactory")
public Object[][] getdata(){
returnnew Object[][] {{"This is Example for DataProvider With Factry"},{"This is another Example for DataProvider With Factry"}};
}

@Test
publicvoid TestMethod(){
System.out.println(msg);
}
}

TestNG.xml

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

Results
image

The example will make TestNG create two test classes, on with the constructor invoked with the value {{"This is Example for DataProvider With Factry"} and the other with {"This is another Example for DataProvider With Factry"}.

Comments