TestNG @Parameters

One of the important features of TestNG is parameterization. This feature allows users 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.

  1. Using @Parameters
  2. Using @DataProvider

1. @Parameters Annotation

If we need to pass some simple values such as String types to the test methods at runtime, we can use this approach of sending parameter values through testng XML configuration files.

The syntax to use the @Parameters is:

@Parameters({ "mapped-param-name" })
@Test
public void prameterTestOne(String param) {
	//
}

In the above example, any value passed to the mapped-param-name will be stored and accessible through the constructor argument param.

The @Parameters annotation can be used the following annotated methods:

  • @BeforeXXX
  • @AfterXXX
  • @Factory
  • @Test

@Parameters can be used to initialize variables and use them in a class, test, or maybe for the whole test suite execution.

2. Demo

Let’s write a simple example of passing parameters to test methods through the XML configuration file.

2.1. Test Class

In the below test, we created a test class with multiple methods that accept parameters from testing suite file. The parameter values have been set at both suite and method level in the testng XML file.

Any parameter value defined at the method level will override the value of a parameter, with same name, if defined at the suite level.

We can see this in test three for the 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);
	}
}

2.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>

2.3. Output

Now run the above tests using testng.xml.

[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
===============================================

3. Optional Parameters

TestNG also provides an option to provide optional parameters, this value will be used if the parameter value is not found in the defined file.

3.1. @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.

3.2. Passing Optional Values

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. Output

The 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 the 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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

12 thoughts on “TestNG @Parameters”

    • You can just send like the below

      <parameter name=”num1″ value=”100″ />

      in the testNG class, you can give like

      @Test()
      @Parameter({"num1"})
      void receiveNum(int num1)
      {
      System.Out.Println("Print number "+num1); // This will print "Print number 100"
      }

      Reply
  1. 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);

    Reply
  2. is it possible to pass a value (set value) to the parameter on the testng.xml?

    <parameter name="userName" value="" />

    can we set the value to that parameter from a class?

    Reply
  3. 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?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.