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:

 

Output:

[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.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning