Using Multiple DataProviders in TestNG

Using Multiple DataProviders in TestNG

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with TestNG in Eclipse, a common challenge is how to utilize multiple DataProviders for a single test method. This article will guide you through the process, providing practical solutions and examples to make your testing more efficient.

Why Combine DataProviders?

Sometimes, you may have separate data sets that you want to test independently but within the same test method. Combining these DataProviders can help streamline your testing process, allowing you to maintain separation of data logic while still achieving comprehensive test coverage.

Step-by-Step Solution

While TestNG does not natively support multiple DataProviders for a single test method, you can merge the data sets from multiple DataProviders into a single DataProvider. Here is a practical example:

Defining DataProviders

First, define your individual DataProviders:

public Object[][] dp1() {
    return new Object[][] {
        new Object[] { "a", "b" },
        new Object[] { "c", "d" },

public Object[][] dp2() {
    return new Object[][] {
        new Object[] { "e", "f" },
        new Object[] { "g", "h" },

Merging DataProviders

Next, merge these DataProviders into one:

public Object[][] dp() {
    List<Object[]> result = Lists.newArrayList();
    return result.toArray(new Object[result.size()][]);

Using the Merged DataProvider

Finally, use the merged DataProvider in your test method:

@Test(dataProvider = "dp")
public void f(String a, String b) {
    System.out.println("f " + a + " " + b);

Advanced Technique using Streams

For a more modern approach, especially if you are using Java 8 or above, you can utilize streams to combine the DataProviders:

public Object[][] combinedDataProvider() {
    // Using stream to combine the two separate data providers.
    return Stream.of(dp1(), dp2())


By combining multiple DataProviders into a single one, you can effectively manage complex data sets and enhance your testing capabilities in TestNG. This approach not only simplifies your test methods but also maintains the integrity of your data logic.

Enhancing Your Test Automation with Repeato

If you’re looking to further streamline your test automation, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android, designed to help you create, run, and maintain automated tests for your apps efficiently. With its intuitive test recorder and AI-based computer vision, Repeato makes it easy to edit and run tests quickly. For advanced testers, Repeato also offers a scripting interface to automate complex use cases. Explore more about Repeato’s capabilities and how it can enhance your testing processes on our documentation page.

Like this article? there’s more where that came from!