top of page

Understanding TestNG Data Providers


TestNG is a popular testing framework for Java applications. One of the powerful features of TestNG is the ability to run the same test method with multiple sets of data. This is achieved through the use of data providers, which are methods that provide data to a test method during execution. In this post, we will explore how to use data providers in TestNG.

Before we start using data providers in TestNG, we need to make sure that we have the following tools installed:
  1. Java

  2. Selenium

  3. Eclipse or any other Java IDE

  4. TestNG

Syntax for TestNG Data Provider:

@DataProvider(name = "name of the data provider")

public Object[][] methodName() {

{ // Values };

return new Object[][] }

Inheriting Data Provider in testNG:(eg:refer below for inheritance in class module02)

TestNG allows us to inherit data providers in test methods so that we can reuse the same data provider method across multiple test classes.It allows us to avoid code duplication and simplifies the maintenance of test code. It also enables us to manage test data more effectively.

Passing multiple parameters in Data provider:

TestNG also allows us to pass multiple parameters to a test method through the use of data providers.This will be useful when we need to test a method with multiple inputs.

By running the above test cases, we will see the console output as below:

Data provider in testNG using excel:

Here we will implement data driven framework .Data Driven framework is used to drive test cases and suites from an external data feed. The data feed can be data sheets like xls, xlsx, and csv files.A Data Driven Framework in Selenium is a technique of separating the “data set” from the actual test case. Since the test case is separated from the data set, one can easily modify the test case of a particular functionality without making changes to the code.

1: Create a test case with TestNG Data Provider.

In the previous test case, we hardcoded the input values passed through a data provider. however , here we are passing the input values through an excel sheet.

In the above code we are using data provider from another class, we need to add data provider class name (DP class )along with method name(login data).

2: Create a Excelsheet.

Download XLSX • 12KB

3: Create functions to Read data from Excel

WebDriver does not support to read data from excel files directly. we need to add the Apache POI dependency to your project. You can add it to your pom.xml file if you're using Maven, or download the JAR files and add them to your class path manually.

Download RTF • 4KB

4: Create a TestNg test case for accepting data from Excel using Data Provider.

In the above code we created separate class under utility package and inherited them with testcases in module02

5: Run the test against the Test Case name in the Test Data file.

By running the above code, we will see the console output as below:

Key points:
  • The @DataProvider annotation is used to create a data provider in TestNG.

  • The name attribute is optional. If you don't provide a name, TestNG will use the name of the method as the name of the data provider.

  • The method must return a two-dimensional object array.

  • The data provider method can provide data to one or more test methods.

  • Each row in the data provider array represents a set of data that will be passed to the test method.

  • TestNG will execute the test method once for each row in the data provider array, passing in the corresponding set of data.


Using data providers in TestNG can save you a lot of time and effort when testing Java applications. With data providers, you can easily test your code with multiple sets of data, without having to write separate test methods for each set. By following the syntax and key points outlined in this post, you'll be able to create and use data providers in your TestNG test suites.


1 commentaire

Good one :)

bottom of page