Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 15, 2016

TestNG DataProvider





TestNG DataProvider is one of the most important features provided by TestNG. By using this feature user can write data-driven tests, that means a test method can run multiple times with different sets of data. In Parameterization using testng.xml file blog we have seen how to parameterize tests using testng.xml file. this feature is also one of the ways to pass the parameters to the test methods. It helps in providing the complex parameters to the test methods as this way is not possible with the testng.xml file.

To use the this feature in your tests you have to declare a method annotated by DataProvider(i.e. @DataProvider(name = “<data-provider name>”)) and then use the said method in the test method using the dataProvider attribute (i.e. @Test(dataProvider = “<data-provider name >”))  in the Test annotation.

1.DataProvider method and its using method are in Same class:

Below is the sample program:

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

public class DataProviderInSameClass
{
    @DataProvider(name = "data-provider")
    public Object[][] dataProviderMethod()
    {
        return new Object[][]
        {
            {"data-one"},
            {"data-two"}
        };
    }
    
    @Test(dataProvider = "data-provider")
    public void testMethod(String data)
    {
        System.out.println("Data Provided by Data Provider is: "+data);
    }
}

The above test class contains a test method which takes one argument as input and prints it to console when executed. A DataProvider method is also available in the same class by using the DataProvider annotation of TestNG. The name of the said DataProvider method is mentioned using the name attribute of the DataProvider annotation. This returns a double Object class array with two sets of data, data-one and data-two.

The DataProvider is to provide parameter values to a test method is defined by giving the name of the data provider using the dataProvider attribute while using the Test annotation.

Execute the program using Right Click on the program and  Run As → TestNG Test.

Below is the output of the program:

Output for the same class

2.DataProvider method and its using method are in Different classes:

Now will see the example for DataProvider and its using methods are in different classes. Though we can use the DataProvider to get the parameters.

Below is the sample program using DataProvider to pass the parameters:

import org.testng.annotations.DataProvider;

public class DataProviderClass
{
    @DataProvider(name = "data-provider")
    public static Object[][] dataProviderMethod()
    {
        return new Object[][]
                {
                    {"data-one"},
                    {"data-two"}
                };        
    }
}

The preceding class only contains the DataProvider method to provide data to a test method. The method returns two sets of data when called.

Below is the sample program to read the parameters:

import org.testng.annotations.Test;

public class DataProviderInOtherClass
{
    @Test(dataProvider = "data-provider" , dataProviderClass = DataProviderClass.class)
    public void testMethod(String data)
    {
        System.out.println("Data Provided by Data Provider is: "+data);
    }
}

The above class contains a test method which takes one argument as input and prints it onto the console when executed. The DataProvider to provide parameter values to a test method is defined by giving the name of the DataProvider using the DataProvider attribute while using Test annotation. As the DataProvider method is in a different class, the class name to refer for getting



the DataProvider is provided to TestNG using the dataProviderClass attribute as seen in the preceding code.

Execute the program using Right Click on the program and  Run As → TestNG Test.

Below is the output of the program:

Output for the different class

3.Multiple Parameters:

In the above example we have seen the data provider with only one parameter. In this will see how to pass multiple parameters to the test method.

Below is the sample program for multiple parameters:

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


public class DataProviderWithMultipleParameters
{
    @DataProvider(name = "data-provider")
    public Object[][] data()
    {
        Object[][] data = new Object[3][3];
        
        data[0][0] = "FName1";
        data[0][1] = "LName1";
        data[0][2] = "Email1";
        
        data[1][0] = "FName2";
        data[1][1] = "LName2";
        data[1][2] = "Email2";
        
        data[2][0] = "FName3";
        data[2][1] = "LName3";
        data[2][2] = "Email3";
        
        return data;
    }
    
    @Test(dataProvider="data-provider")
    public void readDataProviderData(String fName, String lName, String email)
    {
        System.out.println("First Name is: "+ fName);
        System.out.println("Last Name is: "+ lName);
        System.out.println("Email id is: "+ email);
        System.out.println("*********************");
    }
}

Execute the program using Right Click on the program and  Run As → TestNG Test.

Below is the output of the program:

Ouput for Multiple Values

The above test class contains a test method which takes three arguments as input and prints it to console when executed. The DataProvider returns a double Object array with 3 parameters and 3 sets of data. In the object array first value indicates the sets of the data and second value indicated the parameters count. The above object array indicates 3 sets of data with 3 parameters.

The most important thing is the count of the object array second value must be same as the parameter count of the test method.

Conclusion:

1.Can pass the complex data to test methods as parameters using DataProvider.
2.Data Reading Class and Data Provider Class can be in the same class.
3.Data Reading Class and Data Provider Class can be in different classes.



Share this post: on Twitter on Facebook on Google+

TestNG Optional Parameters TestNG Grouping Tests

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023