Factory Annotation in TestNG:
- It is used to run an entire class or different classes, based on Objects returned by Object[] array of factory method.
Eg:
@Factory
public object[] factoryDemo(){
return new Object{
new Class1(),
new Class2(),
new Class3(),
};
}
It will run 3 classes Class1,Class2 and Class3.
-It is same as right clicking on specified classes and run as testNG test.
- If you want to run a class with different set of data, multiple times then you can do so by initializing variables in respective classes.
Eg:
@Factory
public object[] factoryDemo(){
return new Object{
new Class1(1),
new Class1(2),
new Class1(3),
};
}
public Class1{
int param;
public class1(int param){
this.param=param;
}
@Test
public void test1(){
System.out.println("value of param from test1:" + param);
}
@Test
public void test2(){
System.out.println("value of param from test2:" + param);
}
}
- You can do the same thing by setting Factory method as constructor(See in factory.FactorywithDataProviders in eclipse example) or click here
For more information see those videos:
----------------------------------------
0 Comments