SELENIUM TESTNG: Difference between @Factory and @DataProvider:
DataProvider: A test method that uses DataProvider will be executed a
multiple number of times based on the data provided by the DataProvider. The
test method will be executed using the same instance of the test class to which
the test method belongs.
Factory: A factory will execute all the test methods present inside
a test class using a separate instance of the respective class.
TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any number of times. For example, if you have a test to login into a site and you want to run this test multiple times,then its easy to use TestNG factory where you create multiple instances of test class and run the tests (may be to test any memory leak issues).
TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any number of times. For example, if you have a test to login into a site and you want to run this test multiple times,then its easy to use TestNG factory where you create multiple instances of test class and run the tests (may be to test any memory leak issues).
Whereas, dataprovider is used to provide parameters to a test. If you
provide dataprovider to a test, the test will be run taking different sets of
value each time. This is useful for a scenario like where you want to login
into a site with different sets of username and password each time.
@DataProvider
with @Factory:
The @DataProvider feature
can also be used with the @Factory annotation for creating tests at runtime. This can be
done by declaring the @Factory annotation on a constructor of a class or on a regular
method.
Example :
package
com.rameshsoft.rameshselenium;
import
org.testng.annotations.DataProvider;
import
org.testng.annotations.Factory;
import
org.testng.annotations.Test;
public class DataProviderWithFactory
{
private int param_value;
@Factory(dataProvider =
"dataMethod")
public DataProviderWithFactory(int
param) {
this.param_value = param;
}
@DataProvider
public static Object[][]
dataMethod() {
return
new Object[][] { { 0}, { 1} };
}
@Test
public void testMethodOne() {
int value = param_value + 1;
System.out.println("Test
method one output: "+ value);
}
@Test
public void testMethodTwo() {
int value = param_value + 2;
System.out.println("Test
method two output: "+ value);
}
}
output:
Test method one output: 1
Test method one output: 2
Test method two output: 2
Test method two output: 3
No comments:
Post a Comment