One of the important features of TestNG is parameterization. This feature allows user to pass parameters to tests as arguments. This is supported by using the testng @Parameters annotation.
There are mainly two ways through which we can provide parameter values to testng tests.
- Through
testng.xml
XML configuration file - Through
DataProviders
[link]
The @Parameters annotation can be used for any of the @Before, @After, @Factory, and @Test annotated methods. It can be used to initialize variables and use them in a class, test, or may be for the whole test execution.
1. TestNG @Parameters – test parameters with testng.xml
If you need to pass some simple values such as String
types to the test methods at runtime, you can use this approach of sending parameter values through testng XML configuration files. You have to use the @Parameters annotation for passing parameter values to the test method.
@Parameters({ "param-name" })
Let’s write a simple example of passing parameters to test methods through the XML configuration file.
1.1. Tests
In below test, we created a test class with multiple methods that accepts parameters from testng. The parameter values are set at both suite and test level in the testng XML file.
Any parameter value defined at the test level will override the value of a parameter, with same name, if defined at suite level. You can see this in test three for test method prameterTestThree()
.
package com.howtodoinjava.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-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-two-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-param", "test-three-param" }) @Test public void prameterTestThree(String param, String paramTwo) { System.out.println("Test three suite param is: " + param); System.out.println("Test three param is: " + paramTwo); } }
1.2. testng.xml
Now add a testng.xml
file to the project root and put the following code to it. Here we define the parameter values to be passed.
<suite name="Parameter test Suite" verbose="1"> <!-- This parameter will be passed to every test in this suite --> <parameter name="suite-param" value="suite level parameter" /> <test name="Parameter Test one"> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestOne" /> </methods> </class> </classes> </test> <test name="Parameter Test two"> <!-- This parameter will be passed this test only --> <parameter name="test-two-param" value="Test two parameter" /> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestTwo" /> </methods> </class> </classes> </test> <test name="Parameter Test three"> <!-- Overriding suite level parameter --> <parameter name="suite-param" value="overiding suite parameter" /> <!-- Test specific parameter --> <parameter name="test-three-param" value="test three parameter" /> <classes> <class name="com.howtodoinjava.test.ParameterTest"> <methods> <include name="prameterTestThree" /> </methods> </class> </classes> </test> </suite>
1.3. Demo
Now run above tests using testng.xml
. Output of above test run is given below:
[TestNG] Running: C:\somepath\testng.xml Test one suite param is: suite level parameter Test two param is: Test two parameter Test three suite param is: overiding suite parameter Test three param is: test three parameter =============================================== Parameter test Suite Total tests run: 3, Failures: 0, Skips: 0 ===============================================
As you can see from the test results, only timeTestTwo()
for executed because it’s execution time was less than timeout time defined in testng.xml
file. timeTestOne()
execution got cancelled because it took more time to complete than timeout duration configured.
2. TestNG @Parameters – Optional parameters
TestNG also provides an option to provide optional parameters, this value will be used if parameter value is not found in the defined file.
2.1. Test with @Optional annotation
To pass optional parameters, use @Optional
annotation.
package com.howtodoinjava.test; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterTest { @Parameters({ "optional-value" }) @Test public void optionTest(@Optional("optional value") String value) { System.out.println("This is: " + value); } }
The preceding class file contains a single test method that takes one parameter as input. The said test method on execution prints the parameter value that is passed onto the console using the System.out.println
method.
The parameter value is passed to the test method using the parameter named optional-value from the XML file. An optional value for the said parameter is defined using the @Optional
annotation against the said parameter.
2.2. Test with @Optional annotation
In this testng.xml
file has two tests defined above. No parameter is defined in the first test where as the second test declares a parameter named ‘optional-value‘ in it.
<suite name="Optional test Suite" verbose="1"> <test name="Optional Test one"> <classes> <class name="test.parameter.OptionalTest" /> </classes> </test> <test name="Optional Test two"> <parameter name="optional-value" value="passed from xml" /> <classes> <class name="test.parameter.OptionalTest" /> </classes> </test> </suite>
2.3. Demo
Output of running above code as test suite is :
[TestNG] Running: C:\somepath\testng.xml This is: optional value This is: passed from xml =============================================== Optional test Suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
As you can see from the previous test results, TestNG has passed the optional value to the test method during first test execution. This happened because TestNG was unable to find a parameter named optional-value in the XML file from the first test.
During the second test it found the parameter value in the XML and passed the said value to the test method during execution.
Happy Learning !!
prasanth
Is it possible to read a value from a property file and set it as a parameter in xml?
Gagandeep
Is it possible to pass integer values through paramterization? If so, then how?
Madhan
You can just send like the below
in the testNG class, you can give like
Trang
Can Anyone let me know how can I fix for this code
WebElement element = Elements.find({“ID”:params.get(“From ID”),”ID Type”:params.(“From ID Type”)},Browser.Driver);
Uadip
is it possible to pass a value (set value) to the parameter on the testng.xml?
can we set the value to that parameter from a class?
Anupam
How to pass the URL in the TestNG parameter.
raju
if we uncomment the parameter annotation then can we pass values from xml
kiran
i have also same doubt
Chris
Very good explanation, you’re excused the SNAFU pointed out by Karanjeet!
Thanks
Chris
Karanjeet Singh
As you can see from the test results, only timeTestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. timeTestOne() execution got cancelled because it took more time to complete than timeout duration configured.
These above lines were used for explaining “TimeOut” Topic, an di believe, here it is just pasted.
Check it out and Thanks for explanation.
First Time users will be getting an error , after invoking the parameter Test Case, so here is the link which will sort out the problem.
Link : https://stackoverflow.com/questions/32658391/parameter-name-is-required-by-test-on-method-parametertest-but-has-not-been-m
suraj
Thanks for sharing , such informative article really helpful.
can you explain what does it do.
Neeraj
this is use for running a desired method from the class
raj kumar
i am trying to use TestNg for my website testing..but i dont know how to compare result. can you guide me ..
how to check weather my expected result is same as actual result?
Lokesh Gupta
Use : https://www.javadoc.io/doc/org.testng/testng/6.8.17/org/testng/Assert.html