Parameterized Test in TestNG

The name Parameterized Test in TestNG signifies that you need to provide the Test data as parameter to the Test method and process them in script. In most of case you may face some scenario where you need to use huge number of test data to test your business logic.

TestNG feature called Parameterization solve this scenario and gives you the results based on the huge number of test data provided. This can be achieved by using @Parameters annotation. 

You can use huge test data to your test script by using following two ways.

  • By using in testng.xml configuration file
  • BY using DataProviders

In this tutorial we will discuss on the Parameterization implementation using testing.xml file.

By using in testng.xml configuration file:

This is the simple way to implement Parameterization. Testng.xml file is nothing but a XML file which contains all Testing configuration. This XML file also used to organize the test activity like Parameterization, Parallel Test execution etc. TestNG Eclipse plugin creates the Testng.xml file automatically.

Below is the example of sample testing.xml file.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite"> 
<test name="Test"> 
<classes>
 <class name="com.tutorial.testng.RunMultipleTestCasesfromXml"/>
 </classes>
 </test>
 </suite>

In this testing.xml file we can define simple parameters  and reference those parameters to out test class.

Let’s understand this in detail.

Step 1: Create a Java class – ParameterizationTest.java

In first step create the Test case class named ParameterizationTest.java and add the test method testParameter() to it. This method accepts String as Input parameter.

Step 2:

Add @Parameters(“name”) to the method we defined in step 1. The parameter would be passed a value from testng.xml, which we will see in the next step.

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

public class ParameterizationTest {
   @Test
   @Parameters("name")
   public void testParameter(String name) {
      System.out.println("Parameterized value is : " + name);
   }
}

Step 3: Create testing.xml file

After creating the testcase class, create testing.xml file inside the workspace to run the test case

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
   
      <parameter name = "name" value="Pratik"/> 
      
      <classes>
         <class name = " ParameterizationTest " />
      </classes>
      
   </test>
</suite>

Now we have got the clear picture of Parameterized Test in TestNG from the above example

Leave a Reply

Your email address will not be published. Required fields are marked *