Sunday, August 11, 2019

DataProvider using TestNg

DataProvider using TestNg:

Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In this case, you can use a Data Provider to supply the values you need to test.  A Data Provider is a method on your class that returns an array of array of objects.  This method is annotated with @DataProvider:

@DataProvider(name = "testdata")
    public static Object[][] dataProviderMethod()
    {
        return new Object[][] { { "testuser_3", "Test@123"}, { "testuser_4", "Test@123" } };

    }



in the above, dataProvider method we are passing the username and password. 

 We can receive these username and password using below code. 
 @Test(dataProvider="testdata")
 public void TestToFail(String usernName, String passWord) throws IOException {
 E
 System.out.println(usernName);
 System.out.println(passWord);

 }



if the dataProvider method is in same class above code works.

In case if dataProvider in different class we can use below code:

 public class A
{

   @DataProvider(name = "testdata")
    public static Object[][] dataProviderMethod()
    {
        return new Object[][] { { "testuser_3", "Test@123"}, { "testuser_4", "Test@123" } };

    }

}

public class B
{

@Test(dataProvider="testdata",dataProviderClass = A.class)
 public void TestToFail(String usernName, String passWord) throws IOException {
 E
 System.out.println(usernName);
 System.out.println(passWord);

        

 }

}
   
 

No comments:

Post a Comment

If any suggestions or issue, please provide