Setting Up Unique Parameters



HowToDoInJava

/ TestNG / TestNG @Parameters – Test parameters example

Note: As our world comes together to slow the spread of COVID-19 pandemic, the Zoom Support Center has continued to operate 24x7 globally to support you.Please see the updated Support Guidelines during these unprecedented times. Setting Parameters values Add a params argument to render to create a report that uses a new set of parameter values. Here we modify our report to use the aleutians data set with render ('5-parameters.Rmd', params = list (data = 'aleutians')). With the tips above, it’s pretty easy to come up with a password. Just bash your fingers against your keyboard and you can come up with a strong password like 3o(t&gSp&3hZ4#t9. That’s a pretty good one—it’s 16 characters, includes a mix of many different types of characters, and is hard to guess because it’s a series of random characters.

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]

Setting Up Unique Parameters Examples

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.

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

1.1. Tests

Types

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().

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.

1.3. Demo

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

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.

Setting Up Unique Parameters Definition

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.

2.3. Demo

Output of running above code as test suite is :

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?

Setting Up Unique Parameters Types

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

Setting Up Unique Parameters Meaning

TwitterFacebookLinkedInRedditPocket