TestNG Parametrization: Part-2

If you need some complex parameter or parameter that need to created from java then using testng xml file parameter is not sufficient. For this need to use Data provider to pass values into test method. A data provider is a method of your class which return an array of array of objects. TestNg annotation of this method is @DataProvider.

I have created a example for  suppose I have a test method and need to execute with three set of values in this case I use @DataProvider annotation to pass vlaues into test method.

Example:

package com.test;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
       
        @DataProvider(name = "myTest")
        public Object[][] createData1() {
                return new Object[][] {
                  { "Akib", "12345", 1 },
                  { "Aman", "123456", 2},
                  { "Imran", "123456", 3},
                };
        }
         
        @Test(dataProvider = "myTest")
        public void verifyData(String userName,String pass, int id) {
               System.out.println("User name :" + userName + " Paa: " + pass + " Id :" +id );
        }
}

After execution above testng class,  test method  “verifyData” executed three time as Data provider has three set of values. And generated below output

User name :Akib Paa: 12345 Id :1
User name :Aman Paa: 123456 Id :2
User name :Imran Paa: 123456 Id :3
PASSED: verifyData1("Akib", "12345", 1)
PASSED: verifyData1("Aman", "123456", 2)
PASSED: verifyData1("Imran", "123456", 3)


No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution