Parameterization in TestNGParameterization is important in iterating same test with different kinds of data set while executing automation script. Parameterization in testing is achieved using two methods:

  • Using testing.xml file
  • Using DataProviders

Parameterization in TestNG using testng.xml

You can use parameter annotations through the testng.xml file to pass values to test methods as arguments. However, at times it is required to pass values to test methods, especially during the run time. It can be done in the same way as the username and password are passed through testng.xml instead of hard coding it in test methods or as the browser name is passed as a parameter to execute in a specific browser.

Let us now try to understand parameterization with a basic example.

package com.parameterization;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class TestParameters {

     @Parameters({ “browser” })

     @Test

     public void testCaseOne(String browser) {

          System.out.println(“browser passed as :- ” + browser);

     }

     @Parameters({ “username”, “password” })

     @Test

     public void testCaseTwo(String username, String password) {

          System.out.println(“Parameter for User Name passed as :- ” + username);

          System.out.println(“Parameter for Password passed as :- ” + password);

     }

}

In the above class, two parameters ‘username’ and ‘password’ are passed as input to test method ‘testCaseOne’.

The following is the testng.xml file, in which we need to pass the parameter values for the test method.

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

<suite name=”Parameterization Test Suite”>

     <test name=”Testing Parameterization”>

     <parameter name=”browser” value=”Firefox”/>

     <parameter name=”username” value=”testuser”/>

     <parameter name=”password” value=”testpassword”/>

          <classes>

              <class name=”com.parameterization.TestParameters” />

          </classes>

     </test>

</suite>

Passing Parameters with DataProviders

You can use DataProviders to pass complex parameters or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc.). DataProvider uses the annotation @DataProvider with name as one string attribute. However, in case the name is not provided, the method’s name is taken automatically as the data provider’s name. A DataProvider returns an array of objects.

DataProviders can be used using Vector, String or Integer as parameter, as shown below.

@DataProvider(name = “test1”)

   public static Object[][] primeNumbers() {

      return new Object[][] {{2, true}, {6, false}, {19, true}, {22, false}, {23, true}};

   } // This test will run four times since we have defined five parameters.

DataProviders can also be used using object as parameter, as shown below.

   @Test(dataProvider = “test1”)

   public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {

      System.out.println(inputNumber + ” ” + expectedResult);

      Assert.assertEquals(expectedResult,

      primeNumberChecker.validate(inputNumber));

   }

}

You might also like: How to Run testng.xml File Without using IDE or Build Tool?