Make Selenium Easy

TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods

“DataProvider” is an important feature provided by TestNG to provide required test data to test methods to run on. We have learnt different approaches which we can utilize to get best of DataProvider in previous posts.

Let’s learn another way to make more use of DataProvider in TestNG.

Suppose we have three Test methods and each test method requires different data sets to execute. The simplest way to achieve this is by creating a separate DataProvider method for each Test method which means you require three DataProvider method for 3 Test methods. But it will become complex to manage when we have a big count of Test methods.

Solution is Parameterized DataProvider method.

We can create parameterized  DataProvider method in such a way that it will return required data set as per Test method name. Let’s learn how.

We can pass a parameter of type Method which will be the actually currently running Test method and based on name of Test method we can decide what data need to be retuned. We can use switch concept to accommodate more number of test methods. DataProvider method with return type as an Object[] will help us to return different set of data as well. You can use Iterator return type as well. Based on your requirement you can decide it.

As per TestNG official document:

If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for.

Example Program:



[RemoteTestNG] detected TestNG version 6.14.2
Capital of India is New Delhi
Capital of UK is London
Capital of USA is New York
PASSED: India(DataProvider.India@21213b92)
PASSED: UK(DataProvider.UK@6f1fba17)
PASSED: USA(DataProvider.USA@67784306)

    Default test
    Tests run: 3, Failures: 0, Skips: 0

Default suite
Total tests run: 3, Failures: 0, Skips: 0

Hope, you can implement above logic and leverage more advantages of DataProvider methods.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods

  1. I try to execute the test, but show me this error:

    Data Provider public static java.lang.Object[] DataProvider.CustomDataProvider.myDataProvider(java.lang.reflect.Method) must return either Object[][] or Iterator[], not class [Ljava.lang.Object;
    at org.testng.internal.MethodInvocationHelper.invokeDataProvider(
    at org.testng.internal.Parameters.handleParameters(
    at org.testng.internal.Invoker.handleParameters(

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.