TestNG Tutorials 49: Need of DataProvider Method in TestNG

Hello Folks,

TestNG provides a beautiful functionality to parameterized our configuration and test methods so that we can execute the same test script with different set of data. To make a method (Configuration and Test) parameterized , we use “Parameters” annotation. If we make a method parameterized, we must need to pass parameters values from somewhere. TestNG provides below ways to do the same:

  1. Parameters from testng.xmlParameters from DataProvider

We have seen a lot of examples on “Parameters from testng.xml” already. You can go through them here:

TestNG Tutorials 42: Parameters In TestNG or Parameterization of Methods in a TestNG Class

TestNG Tutorials 43: Difference Between “Parameters” and “parameter” in TestNG

TestNG Tutorials 44: Constructor with @Parameter Annotation

TestNG Tutorials 45: Passing Parameters at Test Method Level in TestNG

TestNG Tutorials 46: Overriding Parameters in TestNG

TestNG Tutorials 47: Marking a Parameter as Optional in TestNG

Now question comes here that:

  1. What factors make us to use DataProvider over testng.xml?
  2. When we should go for testng.xml or DataProvider or both?

Both they ways of providing parameters values in TestNG have their own advantages. We will compare them here:

  1. Parameters is a great functionality as it eliminate the need of hard coded data within script. Using testng.xml we can provide data to parameters. But do not you think that testng.xml is related/part to script directly or indirectly? Its ok that data is separated from scripts but can you give this testng.xml to a BA or Manual Testers to update data? They might feel difficult to update an xml.  But if you pass data from an Excel or properties file or Database, it will be easier to update data for anyone as per their required data to run tests. Manual tester or BA can easily update data and execute scripts. Since you can not read data from external sources like Excel, DB etc through parameter tag in testng.xml, we must need to use DataProvider.
  2. If we need to run a test with multiple set of data, we need to use DataProvider. Passing a set of data using testng.xml is not possible. It is also a way of achieving data driven testing with TestNG. But remember DataProvider is not a good way of achieving data driven testing.
  3. If values to parameters are being decided during run time from previous tests, you need to use DataProvider only as you can not modify testng.xml at run time.
  4. DataProvider also provides you flexibility to run a test method for specific set of data using indices. We will see this in upcoming posts.
  5. DataProvider also provides you to run a test method parallely with different set of data. We will see this in upcoming posts.
  6. You can pass value of any type including user defined class type as well which is not possible in Parameters.

I think now you can understand actual usage of DataProvider. We will see a lot about DataProvider in upcoming posts.

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