Parameterization


TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng.xml file. It is very useful when user want to pass values to test methods during run time.

There are two ways to set these parameters: with testng.xml or programmatically(@Data Provider).

Fist we will discuss about Parameterization through TestNG.xml

Lets create a java class

package testngexp;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
publicclassparameterizationexample {
@Parameters({ "MethodName1" })
@Test
publicvoid TestMethod1(String MethodName1){
System.out.println("This TestMethod-->"+MethodName1);
}
@Test
@Parameters({ "MethodName2" })
publicvoid TestMethod2(String MethodName2){
System.out.println("This TestMethod-->"+MethodName2);
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<parameter name="MethodName1" value="TestMethod1"/>
<parameter name="MethodName2" value="TestMethod2"/>
<testname="Test1">
<classes>
<classname="testngexp.parameterizationexample"/>
</classes>
</test><!-- Test -->
</suite><!—Suite -->

Results

image

Note:
1). The XML parameters are mapped to the Java parameters in the same order as they are found in the annotation, and TestNG will issue an error if the numbers don't match.
2). Parameters are scoped. In testng.xml, you can declare them either under a <suite> tag or under <test>. If two parameters have the same name, it's the one defined in <test> that has precedence. This is convenient if you need to specify a parameter applicable to all your tests and override its value only for certain tests.

Example : Define two parameters have same name in <suite> and <Test>

Lets create a java class

package testngexp;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
publicclass parameterizationexample {
@Parameters({ "MethodName1" })
@Test
publicvoid TestMethod1(String MethodName1){
System.out.println("This TestMethod-->"+MethodName1);
}
@Test
@Parameters({ "MethodName2" })
publicvoid TestMethod2(String MethodName2){
System.out.println("This TestMethod-->"+MethodName2);
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<parameter name="MethodName1" value="SuiteTagTestMethod1"/>
<parameter name="MethodName2" value="SuiteTagTestMethod2"/>
<testname="Test1">
<parametername="MethodName1"value="TestTagLevelTestMethod1"/>
<parametername="MethodName2"value="TestTagLevelTestMethod2"/>
<classes>
<classname="testngexp.parameterizationexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results
image

Here, we define parameters in TestNG.xml has same parameter name(MethodName1 and MethodName2) in <suite> and <Test> level, but it taking parameter values from <Test> level only because it’s take <Test> level has precedence.

Passing “Optional” Parameters Through testng.xml

Lets create a java class

package testngexp;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
publicclass parameterizationexample {
@Parameters({ "MethodName1" })
@Test
publicvoid TestMethod1(String MethodName1){
System.out.println("This TestMethod-->"+MethodName1);
}
@Test
@Parameters({ "username" })
publicvoid loginWindow(@Optional("ABCUSer") String username){
System.out.println("Logged in User name is -->"+username);
}
}

TestNG.xml- Passing username Parameter value

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<parameter name="MethodName1" value="SuiteTagTestMethod1"/>
<parameter name="username" value="xyzuser"/>
<testname="Test1">
<classes>
<classname="testngexp.parameterizationexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->


Results
image

Here we are passing parameter value from testng.xml file, so it’s taking from testng.xml file.

Let’s create TestNG.xml file with out usernameparameter in TestNg.xml.

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none"verbose="1"timeOut="1000">
<parameter name="MethodName1" value="SuiteTagTestMethod1"/>
<testname="Test1">
<classes>
<classname="testngexp.parameterizationexample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results
image

Here, in TestNg.xml file we are not defined any parameter for username , but it’s displaying username value as ABCUSer because we are declared parameter as @Optional annotation.if no parameter named "username" is found in your testng.xml file, your test method will receive the default value specified inside the @Optional annotation: "ABCUSer".

Comments