Data Provider in TestNG

Test case parameterization is the technique to execute the same Test Case with different input values. Parameterized tests allow developers to run the same tests over and over again using different values.

 

We will see how to parameterize the Test case in TestNG. There are two ways in TestNG to make the test case parameterized test.

  1. Using @DataProvider

  2. Using testng.xml

Let’s see how we can  make a Test case parameterized in TestNG.

 

Parameterized Test Using @DataProvider

In TestNG, We have an annotation @DataProvider which is used to parameterize the test case. We can use DataProviders when we want read test data from Database, Excel file, XML file or any another medium. Data Provider is a method annotated with @DataProvider. A Data Provider returns an array of objects. So the return type of DataProvider method must be two-dimensional object array.

 

Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

Steps to parameterize the Test–

  1. Create the data provider method with@DataProvider annotations. (Methods return type must be two-dimensional object arrays)
  2. Declare the “Data Provider” name after the @Test annotation. Like – @Test (dataProvider = “data provider method name”)
  3. Pass the arguments in the Test method, what you want to use from the data provider.

Let’s see the example below–

Output:

Username is –> Selenium & Passwprd is –> Selenium@
Username is –> Merilla & Passwprd is –> Selenium@
Username is –> Eden & Passwprd is –> Selenium@
Username is –> Anshul & Passwprd is –> Selenium@

PASSED: loginAccount(“Selenium”, “Selenium@”)
PASSED: loginAccount(“Merilla”, “Selenium@”)
PASSED: loginAccount(“Eden”, “Selenium@”)
PASSED: loginAccount(“Anshul”, “Selenium@”)

 

===============================================
    Default test
    Tests run: 4, Failures: 0, Skips: 0
===============================================

 

 

So you have seen, How data provider function in TestNG. In this testData array, we will read the data from the third external file. We will see this in Selenium Tutorials.

 

Now suppose your “DataProvider” function is in a different class and you want to use it in some another class then, How you will use this. In this case, you just need to specify the DataProvider holder name with data provider name in the @Test annotation.

Example – We have seen our Data Provider function is in LoginParameterized

class, now we want to use this in our another test in some other class namedSampleClass then we need to declare this with our @Test annotation in SampleClass like this —

@Test(dataProvider=”getData”, dataProviderClass=LoginParameterized.class)

 

After this, our test present in SampleClass will execute the DataProvider of class LoginParameterized.

 

Test Parameterization using testng.xml

TestNG allows the user to pass values to test methods as arguments by using @Parameterannotations through testng.xml file.

This technique of parameterization can be used when the scope of Test data is limited. Suppose, We want to pass Browser Name run time i.e on which browser test will run. So, in this case, we can use this kind of parameterization instead of hard code the values in our code.

 

Actually, it functions on the concept of key-value pair. We declare the key within the@Parameters({“key”}) annotations after @Test annotation and declare the values in testng.xml file.

Let’s see how can we parameterize the test case using testng.xml .

 

The structure of testng.xml to pass the value to test method.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” >

  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>

  <test name=”Test With Priority” >
  
  <!–   Declare Parameters –>
    <parameter name=”userName” value=”Selenium”/>
    <parameter name=”password” value=”Selenium@”/>
    
    <classes>
       <class name=”com.Seleniumbix.TestNG.SampleParameterization” />
    </classes>
  </test>

</suite>

Output:

Username is –> Selenium & Password is –> Selenium@

===============================================
TestGroupSuite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

So we have seen both ways to parameterize the test case in TestNG. Practice more to get familiar with these techniques.

2 Comments
  1. Priyanshi Ahuja 10:34 AM / August 22, 2016 - Reply

    Wonderful Tutorial on Test Parametrization …. Thanks

  2. prem singh 1:45 PM / July 16, 2017 - Reply

    nice

Leave a Reply