PAGE-14 : @DATA-PROVIDER AND @FACTORY ANNOTATION

Using @DataProvider along with the @Factory annotation

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.

public class DataProviderTest
{
    private int param;
 @Factory(dataProvider = "dataMethod")
    public DataProviderTest(int param) {
        this.param = param;
    }

    @DataProvider
    public static Object[][] dataMethod() {
        return new Object[][] { { 0 }, { 1 } };
    }
  @Test
    public void testMethodOne() {
        int opValue = param + 1;
        System.out.println("Test method one output: " + opValue);
    }
 @Test
    public void testMethodTwo() {
        int opValue = param + 2;
        System.out.println("Test method two output: " + opValue);
    }
}

The preceding class is similar to the test class, which we used earlier. The constructor of the test class is annotated with the@Factory annotation. This annotation uses a DataProvider method named dataMethod for providing values to the constructor of the test class. The DataProvider method returns a double object array in which the first array represents the dataset, which decides the number of times the test will be iterated, whereas the second array is the actual parameter value that will be passed to the test method per iteration. The said double object array contains two datasets with values 0 and 1.

Let’s run the test now.
Test method one output: 2
Test method one output: 1
Test method two output: 3
Test method two output: 2
PASSED: testMethodOne
PASSED: testMethodOne
PASSED: testMethodTwo
PASSED: testMethodTwo

Dependent tests with the @Factory annotation

We have seen different examples of factory implementation so far. Let’s see how a dependency method is executed when used with the factory class.
public class DependencyTest
{
    private int param;

    public DependencyTest(int param) {
        this.param = param;
    }
 @Test(dependsOnMethods = { "testMethodTwo" })
    public void testMethodOne() {
        System.out.println("Test method one with param values: " + this.param);
    }
 @Test
    public void testMethodTwo() {
        System.out.println("Test method two with param values: " + this.param);
    }
}

public class SimpleTestFactory
{
    @Factory
    public Object[] factoryMethod()
    {
        return new Object[] { new DependencyTest(1), new DependencyTest(2) };
    }
}
This class contains two test methods testMethodOne and testMethodTwo, where testMethodOne depends on testMethodTwo. The constructor of the class takes one argument as integer, and sets its value to an internal variable named param. Both of the test methods print their method name along with the param variable value to console when executed.

Let’s run the tests now.
Test method two with param values: 2
Test method two with param values: 1
Test method one with param values: 2
Test method one with param values: 1
PASSED: testMethodTwo
PASSED: testMethodTwo
PASSED: testMethodOne
PASSED: testMethodOne

As you can see from the previous test results both the instances of testMethodTwo were executed before any instance of testMethodOne. This is the default behavior of a factory implementation in TestNG, it will execute all the instances of the dependent test methods before the actual test method.


Difference between @Factory and @DataProvider
Below is the main difference between @Factory and @DataProvider functionalities on TestNG.

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).
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.



Passing parameters to test classes

One of the main advantage of using the factory methods is that you can pass parameters to test classes while initializing them. These parameters can then be used across all the test methods present in the said classes.
public class SimpleTest
{
 private int param;
 public SimpleTest(int param) {
        this.param = param;
    }

    @Test
    public void testMethodOne() {
        int opValue = param + 1;
        System.out.println("Test method one output: " + opValue);
    }

    @Test
    public void testMethodTwo() {
        int opValue = param + 2;
        System.out.println("Test method two output: " + opValue);
    }
}

public class SimpleTestFactory
{
    @Factory
    public Object[] factoryMethod() {
        return new Object[] { new SimpleTest(0), new SimpleTest(1) };
    }
}
The constructor of the previous test class takes one argument as an integer, which is assigned to a local variable param. This variable then is used in the two test methods present in the test class. Each of the test methods adds a value to param and prints it to the console on execution.

Let’s run the test now.
Test method one output: 2
Test method one output: 1
Test method two output: 3
Test method two output: 2
PASSED: testMethodOne
PASSED: testMethodOne
PASSED: testMethodTwo
PASSED: testMethodTwo


As you can see from the preceding test results, each of the test methods are executed two times each. The parameters passed while initializing the test class are used by the test methods and the console shows the respective output.

No comments:

Post a Comment

About Me

My photo
You can reach me out at : jimmiamrit@gmail.com

Total Pageviews