TestNG @Parameters Annotation
TestNG @Parameters Annotation
The <strong>@Parameters</strong>
annotation in TestNG is used to pass parameter values from the testng.xml
file to your test methods. This is especially useful when you want to run the same test with different sets of input data without using data providers. TestNG allows the user to pass values to test methods as arguments by using parameter annotations.
Some times it may be required for us to pass values to test methods during run time. Like we can pass user name and password through testng.xml instead of hard coding it in test methods. or we can pass browser name as parameter to execute in specific browser.
Steps to define are as follows:
- Define parameters in the
testng.xml
file. - Use
@Parameters
in your test method to accept those values. - The test method parameters must match the names and types defined in the XML.
Example
Let us understand parameterization with the help of an example.
package com.testingdocs.demo;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
<strong>@Parameters</strong>({ "browser" })
<strong>@Test</strong>
public void testCaseOne(String browser) {
System.out.println("Browser :- " + browser);
}
<strong>@Parameters</strong>({ "username", "password" })
<strong>@Test</strong>
public void testCaseTwo(String username, String password) {
System.out.println("Parameter for User Name :- " + username);
System.out.println("Parameter for Password :- " + password);
}
}
In the above class, for Test Method ‘testCaseOne‘, we are passing two parameters ‘username’ and ‘password’ as input to test method.
The below is the testng.xml file, in which we need to pass the parameter values for the test method.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<<strong>suite</strong> <strong>name</strong>="Parameterization Test Suite">
<<strong>test</strong> <strong>name</strong>="Testing Parameterization">
<<strong>parameter</strong> <strong>name</strong>="browser" <strong>value</strong>="Firefox"/>
<<strong>parameter</strong> <strong>name</strong>="username" <strong>value</strong>="sample_user"/>
<<strong>parameter</strong> <strong>name</strong>="password" <strong>value</strong>="sample_password"/>
<<strong>classes</strong>>
<class <strong>name</strong>="com.testingdocs.demo.TestParameters" />
</<strong>classes</strong>>
</<strong>test</strong>>
</<strong>suite</strong>>
In the above testng.xml file, we have two attributes for parameter tag, the name attribute which defines name of the parameter, and the value attribute defines the value of the parameter.
Run the testng.xml file to view the output.
Error
You will get an error if we don’t specify the parameter in testng.xml file to a test method.
In the above example, if you comment ant of the parameter, and try to see the error by executing it.
org.testng.TESTNGException:
Parameter 'browser' is required by @Test on method 'testCaseOne'
but has not been marked @Optional in testng.xml
In testng.xml, parameter values can be set at both suite and test level. If we have two parameters with the same name, the one defined in will have the precedence. If you need to specify a parameter applicable to all your tests and override its value only for certain tests. In this we can specify a parameter applicable to all our tests and override its value ONLY for certain tests.