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:

[java]
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);
}
}
[/java]

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

[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">
<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 –>
[/xml]

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

 

Leave a Reply

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