PAGE-11 : DATA PROVIDER IN TestNG

DataProvider in TestNG

Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method.
The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method.
In the below example we will pass the data from getData() method to data provider. We will send 3 rows and 2 columns ie. we will pass three different usernames and passwords.
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
public class DataProviderExample{
               
               //This test method declares that its data should be supplied by the Data Provider
               // "getdata" is the function name which is passing the data
       // Number of columns should match the number of input parameters
 
               @Test(dataProvider="getData")
               public void setData(String username, String password)
               {
                               System.out.println("you have provided username as::"+username);
                               System.out.println("you have provided password as::"+password);
               }
 
               @DataProvider
               public Object[][] getData()
               {
               //Rows - Number of times your test has to be repeated.
               //Columns - Number of parameters in test data.
               Object[][] data = new Object[3][2];
 
               // 1st row
               data[0][0] ="sampleuser1";
               data[0][1] = "abcdef";
 
               // 2nd row
               data[1][0] ="testuser2";
               data[1][1] = "zxcvb";
               
               // 3rd row
               data[2][0] ="guestuser3";
               data[2][1] = "pass123";
 
               return data;
               }
}
When we execute the above example, we will the get the output as below:
Each data set that we pass will be considered as a test method. As we passed three set of data to the data provider, it will display result as below:

Default suite
Total tests run: 3, Failures: 0, Skips: 0

Output of the Above Program
 
you have provided username as::sampleuser1
you have provided password as::sampleuser1
you have provided username as::testuser2
you have provided password as::testuser2
you have provided username as::guestuser3
you have provided password as::guestuser3
PASSED: testdata("sampleuser1", "abcdef")
PASSED: testdata("testuser2", "zxcvb")

PASSED: testdata("guestuser3", "password123")

No comments:

Post a Comment

About Me

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

Total Pageviews