HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG @Parameters – Test parameters example

TestNG @Parameters – Test parameters example

One of the important features of TestNG is parameterization. This feature allows user 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. Through testng.xml XML configuration file
  2. Through DataProviders [link]

The @Parameters annotation can be used for any of the @Before, @After, @Factory, and @Test annotated methods. It can be used to initialize variables and use them in a class, test, or may be for the whole test execution.

1. TestNG @Parameters – test parameters with testng.xml

If you need to pass some simple values such as String types to the test methods at runtime, you can use this approach of sending parameter values through testng XML configuration files. You have to use the @Parameters annotation for passing parameter values to the test method.

@Parameters({ "param-name" })

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

1.1. Tests

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

Any parameter value defined at the test level will override the value of a parameter, with same name, if defined at suite level. You can see this in test three for 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);
	}
}

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

1.3. Demo

Now run above tests using testng.xml. Output of above test run is given below:

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

As you can see from the test results, only timeTestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. timeTestOne() execution got cancelled because it took more time to complete than timeout duration configured.

2. TestNG @Parameters – Optional parameters

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

2.1. Test with @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.

2.2. Test with @Optional annotation

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

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 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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. prasanth

    January 23, 2020

    Is it possible to read a value from a property file and set it as a parameter in xml?

  2. Gagandeep

    August 12, 2019

    Is it possible to pass integer values through paramterization? If so, then how?

    • Madhan

      August 14, 2019

      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"
      }
      
  3. Trang

    April 23, 2019

    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);

  4. Uadip

    January 30, 2018

    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?

  5. Anupam

    August 23, 2017

    How to pass the URL in the TestNG parameter.

  6. raju

    June 29, 2017

    if we uncomment the parameter annotation then can we pass values from xml

    • kiran

      July 3, 2017

      i have also same doubt

  7. Chris

    March 1, 2017

    Very good explanation, you’re excused the SNAFU pointed out by Karanjeet!

    Thanks
    Chris

  8. Karanjeet Singh

    January 8, 2017

    As you can see from the test results, only timeTestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. timeTestOne() execution got cancelled because it took more time to complete than timeout duration configured.

    These above lines were used for explaining “TimeOut” Topic, an di believe, here it is just pasted.
    Check it out and Thanks for explanation.
    First Time users will be getting an error , after invoking the parameter Test Case, so here is the link which will sort out the problem.

    Link : https://stackoverflow.com/questions/32658391/parameter-name-is-required-by-test-on-method-parametertest-but-has-not-been-m

  9. suraj

    September 4, 2016

    Thanks for sharing , such informative article really helpful.
    can you explain what does it do.

    <include name="prameterTestOne" />
    • Neeraj

      March 2, 2020

      this is use for running a desired method from the class

  10. raj kumar

    April 22, 2015

    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?

    • Lokesh Gupta

      April 22, 2015

      Use : https://www.javadoc.io/doc/org.testng/testng/6.8.17/org/testng/Assert.html

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces