Parameterization using testng.xml file
There are two ways to pass the parameters in the TestNg. For this, need to use Parameters and DataProvider annotations of the TestNG. Parameterization using testng.xml file will allow users to pass parameters to the test methods as an arguments using xml file.
Below are the two ways to pass the arguments.
1.Using testng.xml configuration file.
2.Using DataProvider.
In this, we will see how to pass the parameters to TestNG using testng.xml configuration file.
Using testng.xml configuration file.
Can pass some simple values such as Strings to the test methods at runtime. You can use this approach for sending parameter values through TestNG xml configuration files. For this, you have to use Parameters annotation in the code and Parameter tag in the XML file to pass the parameter values to test methods.
Below is the sample program for using xml file to pass the parameters:
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterTest { /** * Following method takes one parameter as input. * Value of the said parameter is defined at suite level. */ @Parameters({ "suite-level-param" }) @Test public void prameterTestOne(String param) { System.out.println("Test one suite param is: " + param); } /** * Following method takes one parameter as input. * Value of the said parameter is defined at test level. */ @Parameters({ "test-level-param" }) @Test public void prameterTestTwo(String param) { System.out.println("Test two param is: " + param); } /** * Following method takes two parameters as input. * Value of the test parameter is defined at test level. * The suite level parameter is overridden at the test level. */ @Parameters({ "suite-level-param", "test-three-param" }) @Test public void prameterTestThree(String paramOne, String paramTwo) { System.out.println("Test three suite param is: " + paramOne); System.out.println("Test three param is: " + paramTwo); } }
Run the above program using below XML file to pass the parameters:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Parameter test Suite"> <parameter name="suite-level-param" value="Suite Level Parameter" /> <test name="Parameter Test One"> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestOne" /> </methods> </class> </classes> </test> <test name="Parameter Test two"> <parameter name="test-level-param" value="Test Level Parameter" /> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestTwo" /> </methods> </class> </classes> </test> <test name="Parameter Test three"> <parameter name="suite-level-param" value="Overiding Suite Level Parameter" /> <parameter name="test-three-param" value="Test Level Three Parameter" /> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestThree" /> </methods> </class> </classes> </test> </suite>
Below is the output of the above program:
The above xml file contains three tests and each explains different ways of passing the parameters to the test methods. The parameter is declared using parameter tag and name attribute defines the name of the attribute and it should match with the @Parameters annotation attribute in the code. And value attribute defines the value of the parameter. The parameter tag can be used in suite level as well as test level.
If you have a parameter at both suite level and test level then the test level parameter will override the suite level parameter, that means the test level will take high priority.
If you forget to give @Parameters annotation in the program and executed the code using xml file then will get TestNGException. In the above program we will miss the Parameters for the second test and will try to execute the program.
Below is the code for missing Parameter annotation for second test:
import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterTest { /** * Following method takes one parameter as input. * Value of the said parameter is defined at suite level. */ @Parameters({ "suite-level-param" }) @Test public void prameterTestOne(String param) { System.out.println("Test one suite param is: " + param); } /** * Following method takes one parameter as input. * Value of the said parameter is defined at test level. */ //@Parameters({ "test-level-param" }) // Missing Parameters (i.e. Commented) to the test method @Test public void prameterTestTwo(String param) { System.out.println("Test two param is: " + param); } /** * Following method takes two parameters as input. * Value of the test parameter is defined at test level. * The suite level parameter is overridden at the test level. */ @Parameters({ "suite-level-param", "test-three-param" }) @Test public void prameterTestThree(String paramOne, String paramTwo) { System.out.println("Test three suite param is: " + paramOne); System.out.println("Test three param is: " + paramTwo); } }
Then execute the above code using same testng.xml file then will get below output.
It will skip the test method for which we have not provided the parameters and it will say Method “prameterTestTwo” requires 1 parameters but 0 were supplied in the @Test annotation.
In other way if we miss the Parameter tag in the xml file then also will get the TestNGException.
Below is the code for missing Parameter tag (i.e. commenting the code) for second test in the xml file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Parameter test Suite"> <parameter name="suite-level-param" value="Suite Level Parameter" /> <test name="Parameter Test One"> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestOne" /> </methods> </class> </classes> </test> <test name="Parameter Test two"> <!-- <parameter name="test-level-param" value="Test Level Parameter" /> --> <!-- Commenting this line --> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestTwo" /> </methods> </class> </classes> </test> <test name="Parameter Test three"> <parameter name="suite-level-param" value="Overiding Suite Level Parameter" /> <parameter name="test-three-param" value="Test Level Three Parameter" /> <classes> <class name="ParameterTest"> <methods> <include name="prameterTestThree" /> </methods> </class> </classes> </test> </suite>
Then execute the above code using same testng.xml file then will get below output.
It will skip the test method for which we have not provided the parameters tag and it will say Parameter ‘test-level-param’ is required by @Test on method prameterTestTwo but has not been marked @Optional or defined.