Skip to content Skip to navigation
Check Our New Demo Website!

Optional annotation in TestNG

As we know, we can pass parameter values to the test methods during run time from testng xml file by specifying Parameters annotation to test method.

To do this, we need to declare parameters tag in xml file using 'name' and 'value' attribute.Where the name attribute of the tag defines name of the parameter and the value attribute defines the value of the parameter.

If defined parameter is not found in your testng.xml file, The test method will receive the default value which is specified inside the @Optional annotation.

Syntax to define Parameter :

 
<parameter name="param" value="First parameter" />

Syntax to define @Optional annotation

@Parameters("browser")
@Test
public void openBrowser(@Optional("firefox") String value) { ... }

Let us look into the simple example :

package com.easy.param;

import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class OptionalParamExample {

	@Parameters("param one")
	@Test
	public void testOptionParamOne(String paramOne) {

		System.out.println("Parameter passed from XML ::" + paramOne);
	}

	@Parameters("param two")
	@Test
	public void testOptionParamTwo(@Optional("two") String paramTwo) {

		System.out.println("Optional value passed ::" + paramTwo);
	}
}

Below is the testng.xml where we will pass the parameters to test methods

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sample Test Suite" verbose="1" >
  <parameter name="param one" value="First parameter" />
  <parameter name="param two" value="Second parameter" />
  <test name="Method Test Cases" >
    <classes>
       <class name="com.easy.param.OptionalParamExample" />
    </classes>
  </test>
</suite>

When we execute the above program the output should look like below.

Parameter passed from XML ::First parameter
Optional value passed ::Second parameter

===============================================
Sample Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

In the above example, we have defined two parameters in testng.xml file for two test methods. Now let us try to execute by commenting the second parameter in the above xml file and observe the output.

<!--  <parameter name="param two" value="Second parameter" /> -->

After executing testng.xml file, the output should look like below. And if you observe, it will take the value that we defined for Optional.

Parameter passed from XML ::First parameter
Optional value passed ::IamOptional

===============================================
Sample Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Test Frameworks: 

Comments

Hi Team,

Can anyone suggest me how to automate Web services using JAVA/Test NG.
Would be helpful if u quote with an example.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.