Advertisement

Responsive Advertisement

Factory Annotation in TestNG

 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:

video1

video2

----------------------------------------

If you want to run same class or test multiple number of times, we can do in 3 ways:

1.Using different paramet values inside each test in tesng.xml file
2. Using Factory annotation and constructors for the tests to be invoked.
3. Using Factory annotation for construcors of the class which you want to run multiple times

All those 3 ways are explained in video2. 

Post a Comment

0 Comments