DEV Community

Bala Murugan
Bala Murugan

Posted on

Parameterization with DataProvider in TestNG

Overview
Parameterization in TestNG is also known as Parametric Testing which allows testing an application against multiple test data and configurations. Though we have to consider the fact that exhaustive testing is impossible, however, it is necessary to check the behavior of our application against different sets of data that an end-user can pass. Time and manual effort saving have always been a primary reason for automating an application against all possible data combinations.

Hardcoding the test values every time in our test scripts is never said to be a good automation practice. To overcome this, the TestNG framework helps us with a parameterization feature in which we can parameterize different test values and even keep our test data separate from our test scripts.

Let’s consider an example that highlights the need for parameterization in test automation.

There are various websites that behave differently depending upon the different data entered by different end-users. Suppose, there’s a flight ticket booking web application which the end-users are using to check the flight availability for desired dates. We expect our application to show appropriate results according to the different places of origin and destination that the user enters. Hence, to test our application, we would pass different test data against the source and destination place to check if our application gives the correct results instead of the incorrect ones.

Parameterization in TestNG can be achieved in two ways:

Using Parameter annotation with TestNG.xml file
Using DataProvider annotation
In this article, we would be primarily focusing on the use of DataProvider in TestNG.

Significance of DataProvider in TestNG
Many times it so happens that we have to run our test methods against a huge set of test data to monitor application variant responses. In such cases, creating test scripts using @Parameter annotation with XML file might become a tedious process. To bypass this TestNG comes with @DataProvider annotation which helps us to achieve Data-Driven Testing of our application.

The DataProvider in TestNG prepares a list of test data and returns an array object of the same.

It is highly recommended to create a separate class file for TestNG DataProvider, this helps in maintaining the huge test data separately. If the test data values are small in number then you can also setup DataProvider in the same java file in which you have created your test cases.

Syntax of TestNG DataProvider
Alt Text

Different components of the above syntax:
The data provider method is set as a separate function from a test method, hence, it is marked with a @DataProvider annotation with below default parameters provided by TestNG:
name: This highlights the name of a particular data provider. Further, this name is used with the @test annotated method that wants to receive data from the @DataProvider. If the name parameter is not set in @DataProvider, then the name of this data provider will be automatically set as the name of the method.
parallel: If this is set as true, the test method receiving value from the data provider will run in parallel. The default value is false.
Since the TestNG DataProvider method returns a 2D list of objects, it is mandatory to create a data provider method of Object[][] type.
Note: To use DataProvider in TestNG, we need to import TestNG library: org.testng.annotations.DataProvider

Using DataProvider in TestNG framework
Now that we have understood the basic use of TestNG DataProvider, let’s have a look at some practical examples of flight ticket booking with the test data of multiple sources and destinations.

Java Test Class:
Alt Text
Alt Text

Top comments (0)