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

In this post, we will discuss the @Parameters annotation in detail.

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 @Parameters can be used to initialize variables and use them in a class, test, or maybe for the whole test suite execution.

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 with the following annotated methods:

  • All @Before… methods
  • All @After… methods
  • @Factory
  • @Test

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 testng 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 the 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 whereas 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 !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode