TestNG Tutorials 43: Difference Between “Parameters” and “parameter” in TestNG

Hello Folks,

I was asked this question in an interview and I was like “Are there two terms “Parameters” and “parameter” in TestNG?“.

Yes, There are two different terms, “Parameters” and “parameter. We have used both termas many times but didn’t notice minor thing.

“Parameters” is an annotation which allows us to create parameterized methods in a TestNG class. For example:

public class ParametersThroughXML {

	/* We need to add Parameters annotation to read value from testng xml. Note here that
	 * attribute name passed in Parameters must be same as testng xml. 
	*/ 
	@Parameters({"confBeforeParameter"})
	@BeforeMethod
	public void configurationBeforeMethod(String confBeforeParameter)
	{
		System.out.println("Paramters for before configuration method: "+confBeforeParameter);
	}
	
	@Parameters({"testParameters1","testParameters2"})
	@Test
	public void testMethod(String testParameters1, String testParameters2)
	{
		System.out.println("Paramters one for test method: "+testParameters1);
		System.out.println("Paramters two for test method: "+testParameters2);
	}
	
	@Parameters({"confAfterParameter"})
	@AfterMethod
	public void configurationAfterMethod(String confAfterParameter)
	{
		System.out.println("Paramters for after configuration method: "+confAfterParameter);
	}
}

“parameter” is a tag which is used to pass values to parameterized methods of a TestNG class. Example:

<?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" />
		<classes>
			<class name="Parameters.ParametersThroughXML" />
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

Hopefully, you will not be surprised if you face above question. Answer like a pro.

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.
#ThanksForReading
#HappySelenium

 

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