Difference between @Factory and @DataProvider Annotation: We have discussed different topics Of TestNG, But most of the peoples are confused when it comes to finding out what is the difference between @Factory and @DataProvider When to use DataProvider and When to Use @Factory Annotation. So in this post, we are going to discuss these two functionalities.
Difference Between @Factory & @DataProvider Annotation
The annotation like @Factory and @DataProvider are mainly used to reiterate the same test class with different test data. These annotations will help the user to use the same class seamlessly without duplicating the test class code.
Here is the main difference between @Factory and @DataProvider annotation of TestNG:
@DataProvider Annotation
- A test method that uses DataProvider will be executed multiple numbers of times based on the data provided by the DataProvider. That means this annotation parametrizes the particular test method and executes the test number of times based on the data provided by the DataProvider method.
- The condition that needs to be met here is that the method marked as @DataProvider must return a 2D Object array (Object[][]) where each Object[] will be used as the input parameter to an iteration of the test method which uses the data provider.
- The Test method will be executed using the same instance of the test class to which the test method belongs.
@Factory Annotation
- It Can be used to execute all the test methods present inside a test class with multiple sets of data, using the separate instance of the class.
- Using this, we can instantiate a class multiple times rather than just a method.
- The factory method should return an Object[]. This can be an array of Method calls or class objects.
- The Test method will be executed using the separate instance of the respective class.
Let us take the help of Example to understand these topics more clearly:
@DataProvider Annotation Example
public class DataProviderClass { @BeforeClass public void beforeClass() { System.out.println("Before class executed"); } @DataProvider public Object[][] message() { return new Object [][]{{“Mayank” , new Integer (321)}, {“Dileep”, new Integer (282)}}; } @Test (dataProvider=”message”) public void PrintMsg(String name, Integer id) { System.out.println(“Names are: “+name+” “+id); } }
Output:
You can see there that the before class is executed one time, whereas the printing method executed two times because @DataProvider annotation passed two sets of data.
@Factory Annotation Example
Simple Program:
public class SimpleTest { private String param = ""; public SimpleTest(String param) { this.param = param; } @BeforeClass public void beforeClass() { System.out.println("Before SimpleTest class executed."); } @Test public void testMethod() { System.out.println("testMethod parameter value is: " + param); } } public class SimpleTestFactory { @Factory public Object[] factoryMethod() { return new Object[] { new SimpleTest("one"), new SimpleTest("two") }; } }
Output:
Before the SimpleTest class executed.
testMethod parameter value is: two
Before the SimpleTest class executed.
testMethod parameter value is: one
PASSED: testMethod
PASSED: testMethod
If you see the output, we can find that the beforeClass method is executed before the execution of the test method, which represents that factory implementation executes the test method for each instance of the test class.
Let’s go through with another example where we have implemented the @Factory and @DataProvider Ina Single program:
public class TestFactory { @Factory public Object[] factorymethod() { return new Object[]{new DPandFactoryExaple(), new DPandFactoryExaple()}; }} public class DPandFactoryExaple { @DataProvider public Object[][] message() { return new Object [][]{{“Mayank” , new Integer (321)}, {“Dileep”, new Integer (282)}}; } @Test (dataProvider="message") public void PrintMsg(String name, Integer id) { System.out.println(“Names are: “+name+” “+id); } @Test public void PrintSuccessfullMessage() { System.out.println(“Print the successful message”); }}
If we run the above program, then you will find that the test method, which is associated with the @DataProvider that was executed four times that means that it was executed two times for each instance, as we are passing two sets of data that’s why the count is 4. The other @test method is executed two times for both instances. So the total test case execution count is 6.
@DataProvider gives you the power to run a test method with different sets of data, and @Factory gives you the power to run all methods inside a test class with different sets of data. Though you can also test methods with @Factory, it depends on your use case as to which approach fits it better.
Source: Link
Leave a Reply