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.

Unboxing TestNG Parameters and DataProvider Annotations.

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.

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

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

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

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.


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.

 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.

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

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.

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

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.

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.

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.

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.


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,


Leave a Reply