Passing parameters to the test methods in TestNG is explained in this post.

Parameters are used to pass values to the methods, so that we can verify a method with different types of values. We can use any number of parameters on each test method.

  • @Parameters annotation is used to pass parameters to test methods.
  • @Parameters annotation can be placed on any method like @Test, @Before/@After or @Factory annotations.
TestNG Parameters

Following are the two ways to set Parameters in TestNG:

  1. Parameters from testng.xml
  2. Parameters with DataProvider
   Parameters from testng.xml
  • Declare the parameters in testng.xml, it has a name and value attributes.
  • We can declare them either in <suite> tag or in <test> tag.
  • <suite name="Parameter Sample" verbose="1">
      <parameter name="userName" value="Jakson"/>
      <parameter name="password" value="Michel"/>
      <test name="Parameters" preserve-order="true">
      ..
    </suite>
    
  • If two parameters have the same name, the one defined in <test> tag has precedence. This is useful if you need to specify a parameter that is applicable to all  tests and override its value only for certain tests.
  • Specify @Parameter({…}) on test method to receive parameter value from testng.xml
  • @Parameters({"userName","password"})
    @Test
    public void login(String name,String pwd){
        System.out.println("From login ()....");
        System.out.println("UserName passed from testng.xml..."+name);
        System.out.println("Password passed from testng.xml..."+pwd);
    }
    

    In the above code, specified the parameters “name” and “pwd” in login() test method, should receive the values of “userName” & “password” parameters that are declared in testng.xml file.

Note:  Both name of the parameter declared in xml file and name used in @Parameter annotation should be same, otherwise exception will be thrown.

Suppose if you use annotation on login test method, @Parameters({“userName”,”pwd”})

In this, used parameter “pwd” instead of “password” that is not declared in testng.xml.

Below is the exception you will get when executed.

  • org.testng.TestNGException: 
    Parameter 'pwd' is required by @Test on method login but has not been marked @Optional or defined
    in G:\ExploreSelenium\Config\Suites\parametersuite.xml
    

To overcome, make sure use the correct name or mark that parameter as a optional.

Below is the complete code, TestNG class and testng.xml file that you can directly use.

  • Class ‘ParametersTest’ has two test methods  specified  @Parameters annotation.
    • login test – used two parameters, “userName” & “password”
    • optionalParameter test – used one optional parameter “optParam1”

ParametersTest.java

  • package com.exploreselenium;
    
    import org.testng.annotations.*;
    
    public class ParametersTest {
    
        @Parameters({"userName","password"})
        @Test
        public void login(String name,String pwd){
            System.out.println("From login ()....");
            System.out.println("UserName passed from testng.xml..."+name);
            System.out.println("Password passed from testng.xml..."+pwd);
        }
    
        @Parameters({"optParam1"})
        @BeforeMethod
        public void optionalParamTest(@Optional("server details") String dName){
            System.out.println("From optionalParamTest()....");
            System.out.println("Optional parameter value..."+dName);
        }
    
    }
    

 testng.xml file (parametersuite.xml )

  • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    
    <suite name="Parameter Sample" verbose="1">
        <parameter name="userName" value="Jakson"/>
        <parameter name="password" value="Michel"/>
        <test name="Parameters" preserve-order="true">
            <classes>
                <class name="com.exploreselenium.ParametersTest"/>
            </classes>
        </test>
    </suite>
    
  • In above xml file, didn’t declare parameter “optParam1” , but this parameter is specified in optionalParamTest() test method.
  • To overcome the exception we should make that parameter as optional.
Optional Parameter
  • It is used to pass default values to the test method.
  • When no parameter is found in testng.xml file, test method will receive the default value specified inside the @Optional annotation.
  • Optional parameter is used in a method “optionalParamTest” of the above class
  • Parameter “optParam1” is not declared in testng.xml, hence it will use default value while execution.

Below is the result when executed testng.xml (parametersuite.xml)

  • Executes login test method by pass the parameter values “Jakson”, “Michel”
  • Executes optionalParameter test method by pass the default parameter value “server details” as this parameter is not declared in testng.xml file

           TestNG Parameters Results

 Summary:

 In this post, we learned TestNG parameters and how to pass parameters from testng.xml file.

Parameters with Data provider will be discuss in next post.

Execute TestNG Tests From Ant Task
TestNG Dependencies
0