Unboxing TestNG Parameters and DataProvider Annotations

Unlike the old & reliable JUnit Test Framework, the modern day test automation tool, TestNG has built-in support for the data-driven testing. It provides two ways to supply data to the test cases i.e. via TestNG Parameters and DataProvider annotations.

With the addition of these two annotations, TestNG framework filled a significant gap which its predecessor had. One of these annotations adds the ability to use fixed data values in the test cases whereas the other one allows to query values from any external data sources like Excel or the properties files. In the next sections, you’ll see the live usage of both the TestNG Parameters and DataProvider annotations with ready to run examples.

But before we proceed, let’s understand the benefit of the data-driven/parametric testing. Any test automation tool that has both these capabilities can efficiently take care of the following cases.

  • When the tool has to process the large data set as per business requirement.
  • The business logic demands the same test to run over and over again with different values.

Before you dig deep into the world of TestNG Parameters and DataProvider annotations, check out few related blog posts on TestNG framework.

Let’s now start to explore the TestNG Parameters and DataProvider Annotations. Please note that we’ve used the latest version of Eclipse IDE to create and execute the examples referred in this post.

TestNG Parameters and DataProvider Annotations Explained

TestNG Parameters and DataProvider Annotations Explained.

Let’s first see when and how can we use the <@Parameters> annotation in TestNG projects.

1- Pass parameter with <@Parameters> annotation.

With the help of this annotation, you can allow single as well as multiple parameter values to the test methods.

Question: When should you use the <@Parameters> annotation?

Ans. We can use it for the purpose of parameter testing. It is advisable when the data is small and fixed for the test cases.

Question: How can I utilize the <@Parameters> annotation in my project?

Ans. Please follow the below steps to make use of the <@Parameters> annotation.

Step-1) Create a new Java class and name it as <ParametersTesting.Java>.

Step-2) Add following two methods in the class.

i- OpenBrowser()

  • It’ll take a single String type parameter i.e. browserName.
  • Add the annotation @Parameters(“BrowserName”) to this method.

ii- FillLoginForm()

  • It’ll take two String type parameters i.e. UserName and Passcode.
  • Add the annotation @Parameters({ “UserName”, “Passcode” }) to this method.

TestNG Annotation Example.

package com.techbeamers.testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParametersTesting {
	@Parameters({ "BrowserName" })
	public void OpenBrowser(String BrowserName) {
		System.out.println("browser passed as :- " + BrowserName);

	@Parameters({ "UserName", "Passcode" })
	public void FillLoginForm(String UserName, String Passcode) {
		System.out.println("Parameter for User Name passed as :- " + UserName);
		System.out.println("Parameter for Passcode passed as :- " + Passcode);


Step-3) In Eclipse, select the <ParametersTesting.Java> file. Right-click and press the “TestNG >> Convert to TestNG” option to generate the <TestNG.XML> file.

Step-4) In the step-2, you would have seen the parameters associated with the test methods. But we’d not set values for them. It is because we need to specify their values in the <TestNG.XML> file.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameters Testing Test Suite">
	<test name="Parameters Testing">
		<parameter name="BrowserName" value="Firefox"/>
		<parameter name="UserName" value="demo"/>
		<parameter name="Passcode" value="PASSWORD"/>
			<class name="com.techbeamers.testng.ParametersTesting" />

 It’s now time to execute the <TestNG.XML> file. Right-click the XML file from the Eclipse IDE, and press the “Run As >> TestNG Suite” option. After the execution, the output will get displayed like the one given in the below screenshot.

TestNG Parameters and DataProvider Annotations - Parameter Testing

TestNG Parameters and DataProvider Annotations – Parameter Testing


Another interesting fact about the TestNG is that it allows passing optional parameters using the <@Optional> annotation.

Question: What is <@Optional> annotation and how to use it in TestNG project?

Ans. You can use it to specify an optional value for a parameter which is not available in the <TestNG.XML> file.

Refer the below <@Optional> annotation example.

package com.techbeamers.testng;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class OptionalTesting 
    @Parameters({ "optional-value" })
    public void optionCheck(@Optional("Optional Value") String value) {
        System.out.println("This is: " + value);

Here is the “TestNG.XML” associated with the above example.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Optional Testing Test Suite" verbose="1">
  <test name="Optional Testing Part1">
      <class name="com.techbeamers.testng.OptionalTesting" />
  <test name="Optional Testing Part2">
    <parameter name="optional-value" value="Provided from XML" />
      <class name="com.techbeamers.testng.OptionalTesting" />

You can check from the above <testng.xml> file that it has two test methods defined in it. The first test doesn’t have any parameter while the second one specifies a parameter named as “optional-value.”

After running the <testng.xml> as a test suite, the output would be as follows.

[TestNG] Running:

This is: Optional Value
This is: Provided from XML

Optional Testing Test Suite
Total tests run: 2, Failures: 0, Skips: 0

If you observe the test results, TestNG has used the optional value while executing the first test method. It happened because TestNG couldn’t find a parameter named as “optional-value” in the XML file for the first test. However, for the second test, it resolved the parameter value which also gets printed during the test execution.

For your note, you can use the parameter annotation with any of the Before/After, Factory, and Test annotated methods. Additionally, you can utilize it to set variables and use them in class, test, or test suite.


2- Pass Parameter with <@DataProvider> annotation.

The data provider is another annotation which supports data-driven testing. You can use it to handle a broad range of complex parameters like the following.

  • Java objects.
  • Objects read from a database.
  • Data from Excel or property file etc.

Below are some interesting facts about the data provider.

  • This annotation has one string attribute which is its name. If you don’t specify a name, then the method’s name serves as the default name.
  • A data provider method prepares and returns a 2-d list of objects.
  • A data-driven test would run once for each set of data specified by the data provider object.

Now let’s view the steps required to use the data provider annotation for data-driven testing.

  • Create a new Java class, say, the DataProviderTest.Java.
  • Define the data provider method annotated using the <@DataProvider>. It should return the 2-d list of objects.
  • Add a test method and decorate it using the <@Test(dataProvider = “name of data provider”)>.

For more clarity on the data provider annotation, read the below code example very carefully. In this code example, we are demonstrating the three different usages of data providers.

  • Passing Java integer object using the data provider.
  • Streaming Java bean object using the data provider.
package com.techbeamers.testng;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {

	// =================================================================
	// Odd Digit Test
	public class OddDigit {
		public Boolean validate(int num) {

			return (num % 2 == 0 ? false : true);

	private OddDigit oddDigit;

	public void initialize() {
		oddDigit = new OddDigit();

	@DataProvider(name = "OddDigitTest")
	public static Object[][] getNumbers() {
		return new Object[][] { { 1, true }, { 7, true }, { 10, false },
				{ 12, false }, { 15, true } };

	// This test will run 5 times as we are passing 5 parameters.
	@Test(dataProvider = "OddDigitTest")
	public void testPrimeNumberChecker(Integer inputNumber,
			Boolean expectedResult) {
		System.out.println(inputNumber + " " + expectedResult);
		Assert.assertEquals(expectedResult, oddDigit.validate(inputNumber));

	// =================================================================
	// Java Bean Object Test
	public static class JBean {
		private String type;

		public JBean(String type) {
			this.type = type;

		public String getVal() {
			return type;

		public void setVal(String type) {
			this.type = type;

	@DataProvider(name = "JavaBeanObjectTest")
	public static Object[][] getJBeans() {
		return new Object[][] { { new JBean("Stateless Session Beans") },
				{ new JBean("Stateful Session Beans") } };

	@Test(dataProvider = "JavaBeanObjectTest")
	public void testMethod(JBean myBean) {

You can run the above code from Eclipse as a TestNG Test. Or you can generate the <TestNG.XML> and then run the XML file as a TestNG Suite.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="DataProviderSuite" parallel="tests">
  <test name="DataProviderTest">
      <class name="com.techbeamers.testng.DataProviderTest"/>
  </test> <!-- DataProviderTest -->
</suite> <!-- DataProviderSuite -->

After you execute the above code either as a test or as a test suite, you’ll see the following output. Please verify from the below snippet.

[TestNG] Running:

Stateless Session Beans
Stateful Session Beans
1 true
7 true
10 false
12 false
15 true

Total tests run: 7, Failures: 0, Skips: 0


Final Word.

We’ve tried to cover as much as we could about the TestNG Parameters and DataProvider annotations along with their examples. Though, there are many other data provider use cases that we would address in a separate post. Till then enjoy reading this post and share it on social media.


All the Best,