TestNG Dataproviders

In many scenarios, a test must be executed with a large set of input data to validate how the application responds to different conditions. Writing separate tests or relying on XML parameterization can become repetitive and hard to manage.

To simplify this, TestNG provides the @DataProvider annotation, which supports parameterizing test methods dynamically. The data provider method returns a 2D object array, where each row is a set of parameters for one execution of the test.

Note: 

It’s best practice to place data providers in a separate class if the test data is large.
For smaller datasets, you can define the data provider within the same class as your test.

Syntax - @DataProvider

@DataProvider(name = "nameOfDataProvider")
public Object[][] dataProviderMethod() {
    return new Object[][] {
        // Sets of values go here
    };
}


Key Points to Remember

     1. The DataProvider annotation has a single attribute called name, which you can select as per your convenience.
     2. DataProviders are separate methods used in test functions, which means that this annotation is not used on test functions like the testNG parameters.
     3. The @DataProvider method must return an Object[][] (two-dimensional array).
     4. Each row represents one execution of the test method.
     5. Each column in a row corresponds to a parameter in the test method.
     6. If the name attribute is omitted, the method name is used as the provider name.
     7. The @DataProvider can be in the same class or a different class (via dataProviderClass).
     8. To use DataProvider in TestNG, we need to import TestNG library: org.testng.annotations.DataProvider

Parameterization with DataProvider

Parameterization (also known as Parametric Testing) means running the same test against multiple test data and configurations. Through this we can parameterize different test values and even keep our test data separate from our test scripts.

Example Scenario

Imagine a web-based flight ticket booking application where users search for available flights based on their source and destination cities along with travel dates.

To ensure the application behaves correctly, it must:

Display accurate results based on the input (source and destination),
Handle various combinations of locations properly,
And return correct vs. incorrect results based on different input data.
To test this thoroughly, we need to check the application with multiple input values such as:

Chennai → Mumbai
Delhi → Bangalore
Hyderabad → Kolkata
…and so on.

The Need for Parameterization

Running the same test logic with different sets of input data is called parameterization. In our case, we want to test the same flight search functionality with multiple source and destination values.

How Can You Achieve Parameterization in TestNG?

TestNG offers two primary ways to pass external data into test methods:

A. Using the @Parameters annotation with a TestNG XML file

     You define input values in the XML file and pass them into test methods.
     This works well for a small number of parameters.
     However, when the dataset grows large, managing it via XML becomes manual, repetitive, and error-prone.

<suite name="FlightBookingSuite">
  <test name="SearchTest">
    <parameter name="source" value="Chennai"/>
    <parameter name="destination" value="Mumbai"/>
  </test>
</suite>


B. Using the @DataProvider annotation (Preferred for large test data)

     @DataProvider allows you to pass multiple sets of parameters to a single test method programmatically.
     This method is scalable and better suited for data-driven testing where you want to execute the same test with different combinations of input.
     You write a method that returns an Object[][], where each row is a test case

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class FlightSearchTest {
    @DataProvider(name = "flightData")
    public Object[][] getFlightData() {
        return new Object[][] {
                {"Chennai", "Mumbai"},
                {"Delhi", "Bangalore"},
                {"Hyderabad", "Kolkata"}
        };
    }
    @Test(dataProvider = "flightData")
    public void testFlightSearch(String source, String destination) {
        System.out.println("Searching flights from " + source + " to " + destination);
    }
}


Output

Searching flights from Chennai to Mumbai
Searching flights from Delhi to Bangalore
Searching flights from Hyderabad to Kolkata

 

Related Tutorials