TestNG Tutorials 45: Passing Parameters at Test Method Level in TestNG

Hello folks,

TestNG allows us to create parameterized methods in a TestNG class. A TestNG class may contain more than one @Test annotated methods and every test method may accept zero or more parameters. We can pass those parameters as a whole at “suite” level or “test” level.

Sometimes, it becomes confusing when there are a large number of parameters. It becomes difficult to manage and  identify related parameters belong to test methods.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<parameter name="confBeforeParameter" value="confBeforeParameter" />
		<parameter name="testParameters1" value="testParameters1" />
		<parameter name="testParameters2" value="testParameters2" />
		<parameter name="confAfterParameter" value="confAfterParameter" />
		<parameter name="confBeforeParameter4" value="confBeforeParameter" />
		<parameter name="testParameters14" value="testParameters1" />
		<parameter name="testParameters24" value="testParameters2" />
		<parameter name="confAfterParameter4" value="confAfterParameter" />
		<parameter name="confBeforeParameter5" value="confBeforeParameter" />
		<parameter name="testParameters15" value="testParameters1" />
		<parameter name="testParameters25" value="testParameters2" />
		<parameter name="confAfterParameter5" value="confAfterParameter" />
			<class name="Parameters.ParametersExampleAtClonstructor" />
	</test> <!-- Test -->
</suite> <!-- Suite -->

Bulky and confusing. Isn’t it??

Do you know that we can pass parameters at individual method levels as well. It helps us to keep parameters information near to method to which it belong. But it is slightly different from passing at suite and test level.

Let’s learn it now.

A sample TestNG class:

package Parameters;

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

public class ParametersAtMethodLevel {

	 * Two test methods accepting parameters/arguments
	public void testMethod1(String testParameters1, String testParameters2)
		System.out.println("Paramters one for test method 1: "+testParameters1);
		System.out.println("Paramters two for test method 1: "+testParameters2);
	public void testMethod2(String testParameters1, String testParameters2)
		System.out.println("Paramters one for test method 2: "+testParameters1);
		System.out.println("Paramters two for test method 2: "+testParameters2);

Testng XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
			<class name="Parameters.ParametersAtMethodLevel">
				<!-- Including individual method -->
					<include name="testMethod1"></include>
					<!-- Passing parameters for first method-->
						<parameter name="testParameters1" value="testParameters 1" />
						<parameter name="testParameters2" value="testParameters 2" />
					<!-- Including another method -->
					<include name="testMethod2"></include>
					<!-- Passing parameters for second method-->
						<parameter name="testParameters3" value="testParameters 3" />
					    <parameter name="testParameters4" value="testParameters 4" />
	</test> <!-- Test -->
</suite> <!-- Suite -->

Note here, unlike way of passing parameters at suite and test level, we need to pass parameters at method level after include tag.


More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning