How to use TestNG Parameter and Dataprovider?

April 9, 2024
5 min read
Share this post

Hello everyone, in this blog post,we will be discussing what and why of TestNG Parameters. All you must be aware of what is parameterization and why it is important. Parameterization is a very important component during Development and Testing. Parameterization promotes the idea of Reusability. Reusability, What does that mean? Let's Look at an example-

Suppose we want to write a code to add 2 numbers. Numbers can be an integer or floating-point. Where you can use permutation to generate several different combinations like :

  1. The first number is a floating-point and the second number is an integer
  2. The first number is integer second is floating point
  3. Both are integer
  4. Both are floating-point.

Thus, instead of writing so many methods, we can simply write a single method where parameters are passed dynamically.
Similar is the case for testing where Parameterizations helps us to run test case multiple numbers of times with different inputs and validation values. This concept is known as Data-Driven Testing.

Selenium Webdriver is an automation framework rather than a ready-to-use the tool, so you cannot expect it to be prepared for use without putting in any effort.

There are 2 ways that could figure out how to pass parameters to the test case:

  1. Storing the parameters in the Excel file.
  2. Another most commonly used method is to use TestNG. TestNG lets you pass parameters directly to the test methods using testng.xml.

How to Pass TestNG Parameters:

There are 2 ways to pass parameters:

  1. @Parameters Annotation: Use the TestNG.xml file to pass the simple parameter by value.
  2. @DataProviders Annotation: Use Data Providers to pass complex parameters.

Let's discuss all of the above 2 methods one by one.

1. @Parameters Annotation: Use TestNG.xml to pass Parameters by Value:

a. Launch Eclipse and Create a New Java Project by File->New->Java Project. For this example, I have named the project “SampleTestNG”.
b. Right-click on the project->New->Package. This will let you create a new package. Name this package as “TestNGParameterization”.


c. Right-click on the project “SampleTestNG”->New->Class. I have named the class file as “ParametersTesting”.


d. Now add the following code to the Class file created in the above step:

We have created 3 methods in the Java Class file:

  1. OpenBrowser(): This method will take an argument specifying the browser to be used.
  2. OpenWebsite(): This method will take an argument specifying the website to launch.
  3. FillLoginForm(): This method will take 2 arguments specifying the username and password to be used to login to the website.

e. Right-click the Class file “ParametersTesting”->TestNG->Convert To TestNG.


f. This step will generate the “TestNG.xml” file.


g. Add the following code to the xml file.

We have specified the values of the parameters in the xml file. Thus, you can see we are passing the parameters by value here using the testng.xml file.

h. Right click the xml file->Run As->TestNG Suite.


2. @DataProvider Annotation: Use Data Provider to pass Complex Parameters
When you need to pass complex parameters or to pass the parameters that are created from Java, we create a complex object or objects read from a property file or database, in such case, parameters are not passed using testng.xml. They need to be passed using Data Providers.
Data Provider is a method annotated with @DataProvider. A Data Provider returns an array of objects.

Features of DataProvider Annotation:

1. DataProvider Annotation returns a 2-d list of objects.

2. The test will be written for each set of object specified by the data provider object. Suppose the DataProvider object has 10 sets of more usage of objects in the sentence, so the test will run 10 times.

3. The DataProvider Annotation has one attribute name Not clear. If you don’t specify or forgot to specify the name, in that case, the name of the method becomes the default choice.

Steps to Follow:

1. Right-click on the package “TestNGParameterization” created in the @Parameters Annotation section. Click on New->Class. Name this Class file as “DataProviderTesting”.

2. Add the following code to the Class file:

a. Here our aim is to check whether the number specified is an even number or not.
b. For this, we have specified a list of data objects, specifying the number and expected result as True or False.
c. We have user assert.equals() to check whether the expected result matches the actual value. The actual value is estimated using the Conditional Operator ?:

3Right-click “” Class file->TestNG->Convert to TestNG.

4.Edit the code of the testng.xml file as follows:

5..Right-click testng.xml file->Run As->TestNG Suite.


I hope with this blog post, you must have understood how to use different Annotations to pass Parameters using TestNG. We will be back with some interesting facts and coding examples. Till then, Stay Tuned and Keep Practicing.

Happy Coding!!!

Rupesh Garg
Rupesh Garg
CEO and Chief Architect
Our blog

Latest blog posts

Discover the latest in software testing: expert analysis, innovative strategies, and industry forecasts
Software Testing
Performance Testing

Software Testing Trends for California in 2024

Rupesh Garg
Rupesh Garg
June 20, 2024
5 min read
Software Testing
Testing Tools

A Complete Guide to Troubleshooting Common Issues with JMeter

Rupesh Garg
Rupesh Garg
June 14, 2024
5 min read
Software Testing
Performance Testing

7 Top Software Testing Trends to Watch Out for in 2024

Rupesh Garg
Rupesh Garg
June 6, 2024
5 min read