Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between DataProvider and Factory in TestNG?

Tags:

java

testng

When to use DataProvider and when to use Factory ?

like image 365
why Avatar asked Mar 20 '11 12:03

why


People also ask

What is the difference between DataProvider and parameter in TestNG?

What is the difference between DataProvider and Parameter in TestNG? DataProviders pass the different parameters on a single test in a single execution, whereas parameters pass the parameters just once per execution in TestNG.

What is factory TestNG?

@Factory annotated method allows tests to be created at runtime depending on certain data-sets or conditions. The method must return Object[].

What is DataProvider annotation in TestNG?

@DataProvider annotation helps us write data-driven test cases. The @DataProvider annotation enables us to run a test method multiple times by passing different data-sets.

How do I use DataProvider in Beforetest?

Your DataProvider would basically provide values that your factory method requires to create TestClass instances. Within your TestClass instance you can then have a @BeforeClass or a @BeforeMethod work with the values received while instantiation and proceed.


2 Answers

TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class any no 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.

public class WebTestFactory {      
  //createInstances method will create 10 objects of WebTest class
  @Factory     
  public Object[] createInstances() {      
   Object[] result = new Object[10];       
   for (int i = 0; i < 10; i++) {      
      result[i] = new WebTest(i);      
    }      
    return result;     
  }  

and the test class is now:

public class WebTest {     
  private int m_numberOfTimes;     
  public WebTest(int numberOfTimes) {      
    m_numberOfTimes = numberOfTimes;       
  }    

  @Test    
  public void testServer() {       
   //Code to test the application   
  }    
}    

Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime:

<class name="WebTestFactory" />  

The factory method can receive parameters just like @Test and @Before/After and it must return Object[]. The objects returned can be of any class (not necessarily the same class as the factory class).

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.

public class DataProviderTest {

    @Test(dataProvider= "data")
    public void TestUltimatixFromExcelData(String userName,String password) {
        WebDriver driver; 
        driver=new FirefoxDriver();
        //Test to login into a site
    }

    @DataProvider(name="data")
    public static Object[][] dataProviderTest() throws Exception{

        Object[][] returnArray={new Object[]{"username1","password1"},new Object[]{"username2","password2"},new Object[]{"username3","password3"}
        };
        return returnArray;
    }

}
like image 55
Sreya P.K Avatar answered Oct 20 '22 11:10

Sreya P.K


Data provider always create the same data set. So if you need Person instance you will always get person called John Wayne from data provider. They provide static data. This is good for test parametrization when you supply your test with two objects - first is method input, second that you expect.

Factories allow you to create tests dynamically.. They provide dynamic data like random content or if you want call some method with diffrend parameters.

like image 37
Koziołek Avatar answered Oct 20 '22 10:10

Koziołek