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" })
	@Test
	public void testCaseOne(String browser) {
		System.out.println("browser passed as :- " + browser);
	}

	@Parameters({ "username", "password" })
	@Test
	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 "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
	<test name="Testing Parameterization">
	<parameter name="browser" value="Firefox"/>
	<parameter name="username" value="testuser"/>
	<parameter name="password" value="testpassword"/>
		<classes>
			<class name="com.parameterization.TestParameters" />
		</classes>
	</test>
</suite>

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:

org.testng.TESTNGException:
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.

org.testng.TESTNGException:
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: 

Comments

Hello,

Thanks for the info. I have been trying to use multple parameters but I keep getting the error
"Parameter 'browser' is required by @Test on method 'testCaseOne' but has not been marked @Optional or defined in testng.xml"
If i use only one parameter (browser) I am not getting this error. Can you please help me on this?

Mani

Man you have to specify the values in the TestNg xml file, you can pass value directly in java file, you have to specify a name in java file and link a value to it in testng

"<!-- <parameter name="browser" value="Firefox"/> -->" when i commented the browser ,I got the result for username and password but no error message for browser..

If at all i want to pass the parameter value("FireFox" in the above exmaple) from my command prompt how can i do that.
Help will be great appricated

Can i pass multiple values to same method, for example i would like to execute the same method with different set of parameter (chrome firefox, Ie)

Use @DataProvider in that case and create multiple data sets, and provide data provider class and data Provider name in the @Test( //here).

How can we test same parameter with different values in different environment like QA and Prod

how can we use same parameters for different test cases in different classes without rewriting the parameters in xml file.can we make them global and how can we do that .

how can we use same parameters for different test cases in different classes without rewriting the parameters in xml file.can we make them global and how can we do that .

You've missed writing text @ XXX- If we have two parameters with the same name, the one defined in XXXX will have the precedence??

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.