TestNG Parameterization (2024) | TechGeekNext

TestNG Parameterization (2024)

In this tutorial, we will learn about the TestNG Parameterization and it's different ways to implement.

Q: What is the TestNG Parameterization?
Ans:

The TestNG Parameterization is one of the features of TestNG provides one can run the same test over and over again with different values. In simple words, TestNG Parameters are the arguments that are implemented in the test methods.

Q: How many types of TestNG Parameters?
Ans:

There are two types of TestNG Parameters, they are:

  1. TestNG Parameters using Testng.xml file.
  2. TestNG Parameters using data providers.

Uses of TestNG Parameters

In TestNG parameters are used as numbers of test cases are run with multiple input values so for keeping the test case result successfully. Every time a Hardcode test is not possible the TestNG Parameterization is helpful in testing because if one change is required, the whole test script is to be edited.

TestNG Parametrization use makes the testing easy and makes it in a more organized form.

Example of TestNG Parameter using testng.xml file

Here TestNG parameters using the testng.xml file are applied inside the tag. If parameters are applied to all the test cases then it is applied inside the tag and if parameters are related to the specific folder then applied within a tag.

Create Test Class

Create test class to access parameter from testng.xml files using @Parameters annotation.

package com.techgeeknext.parameterization;

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

public class TestDebit {
	@Test
	@Parameters({ "balance", "amount" })
	public void add(int balance, int amount) {
		int total = balance - amount;
		System.out.println("TestDebit- Total balance amount :
        " + balance + "-" + amount + " = " + total);
	}
}

package com.techgeeknext.parameterization;

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

public class TestCredit {
	@Test
	@Parameters({ "balance", "amount" })
	public void add(int balance, int amount) {
		int total = balance + amount;
		System.out.println("TestCredit- Total balance amount :
        " + balance + "+" + amount + " = " + total);
	}
}

testng.xml

In this case parameters are applicable to all test cases.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">

	<parameter name="balance" value="100"/>
    <parameter name="amount" value="50"/>

	<test name="TestCredit">
		<classes>
			<class name="com.techgeeknext.parameterization.TestCredit" />
		</classes>
	</test> <!-- Test -->

	<test name="TestDebit">
		<classes>
			<class name="com.techgeeknext.parameterization.TestDebit" />
		</classes>
	</test> <!-- Test -->

</suite> <!-- Suite -->
output: Run testng.xml. TestNG Parameter

Example of specific parameters for particular test case using above testng.xml file.

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

	<test name="TestCredit">
	<parameter name="balance" value="200"/>
    <parameter name="amount" value="40"/>
		<classes>
			<class name="com.techgeeknext.parameterization.TestCredit" />
		</classes>
	</test> <!-- Test -->

	<test name="TestDebit">
	<parameter name="balance" value="300"/>
    <parameter name="amount" value="20"/>
		<classes>
			<class name="com.techgeeknext.parameterization.TestDebit" />
		</classes>
	</test> <!-- Test -->

</suite> <!-- Suite -->
output: Run testng.xml. TestNG Parameter

Q: What are data providers in the TestNG Parameters?
Ans:

In TestNG, Data providers help to proceed parameter values directly to the test method. It permits the users to write data-driven tests where they can run multiple times the same test method with different sets of test data.

Data providers parameters are used when complex parameters are passed to the created Java objects or objects read file from a database or property. Data Provider is denoted as @DataProvider. An array of the object is returned by the Data Provider.

Example of using Data Providers in the TestNG

package com.techgeeknext.parameterization;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestLoginData {
	@Test(dataProvider = "getUserData")
	public void testData(String user, String address) {
		System.out.println("userName: "+user +" password: "+address);

	}

	@DataProvider
	public Object[][] getUserData() {
		Object[][] userData = new Object[4][2];
		userData[0][0] = "TechGeekUser1";
		userData[0][1] = "AAA";

		userData[1][0] = "TechGeekUser2";
		userData[1][1] = "BBB";

		userData[2][0] = "TechGeekUser3";
		userData[2][1] = "CCC";

		userData[3][0] = "TechGeekUser4";
		userData[3][1] = "DDD";

		return userData;
	}

}
output: TestNG Parameter

Recommendation for Top Popular Post :