Parameterization in TestNG using testng.xml

TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng.xml file.

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 testmethods. or we can pass browser name as parameter to execute in specific browser.

Let us now try to understand parameterization with a basic example.

package com.parameterization;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParameters {
	@Parameters({ "browser" })
	public void testCaseOne(String browser) {
		System.out.println("browser passed as :- " + browser);

	@Parameters({ "username", "password" })
	public void testCaseTwo(String username, String password) {
		System.out.println("Parameter for User Name passed as :- " + username);
		System.out.println("Parameter for Password passed as :- " + 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 "">
<suite name="Parameterization Test Suite">
	<test name="Testing Parameterization">
	<parameter name="browser" value="Firefox"/>
	<parameter name="username" value="testuser"/>
	<parameter name="password" value="testpassword"/>
			<class name="com.parameterization.TestParameters" />

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.

Now after running the above testng.xml file, the output will show as below:

testng parameterization

We will get an error if we dont 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.

<!-- <parameter name="browser" value="Firefox"/> -->

The below is the exception that you see after execution:

Parameter 'browser' is required by @Test on method 'testCaseOne' but has not been marked @Optional in testng.xml

The @Parameters annotation can be placed on any method that has a @Test, @Before/After or @Factory annotation.

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.

Parameter 'browser' is required by @Test on method 'testCaseOne' but has not been marked @Optional or defined 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.

Test Frameworks: 


Can someone please explain the parameters with same name in different test cases in TestNG?

Add new comment