Parameter Passing in TestNG for LambaTest

How can we pass the parameter to the test script using TestNG?

1 Like

Hii Michael,

In TestNG, you can pass parameters to a test script using the @Parameters annotation along with the ‘parameter’ tag in the testng.xml configuration file. This enables you to dynamically provide inputs to your test methods. Here’s a detailed explanation:

  1. Using @Parameters Annotation:

    In your test script, annotate the test method with @Parameters and specify the parameter name as the annotation value. This links the test method with the parameter defined in the testng.xml file.

    public class TestFile {
       @Test
       @Parameters("sampleParamName")
       public void parameterTest(String paramValue) {
          System.out.println("Value of sampleParamName is - " + paramValue);
       }
    }
    
  2. Configuring Parameters in testng.xml:

    In the testng.xml file, define the parameter values using the ‘parameter’ tag within the ‘test’ or ‘suite’ elements. This is where you set the values that will be passed to the corresponding test methods.

    <suite name="sampleTestSuite">
       <test name="sampleTest">   
          <parameter name="sampleParamName" value="sampleValue"/>
          <classes>
             <class name="TestFile" />
          </classes>      
       </test>
    </suite>
    

    Here, the ‘sampleParamName’ parameter is assigned the value ‘sampleValue’.

  3. Execution Flow:

    When you run your TestNG suite, it will recognize the parameter configuration in the testng.xml file and pass the specified values to the corresponding test methods.

    This dynamic parameterization enhances the flexibility of your test scripts, allowing you to run the same test with different input values without modifying the test code.

By incorporating these practices, you can streamline your testing process and make your test suite more adaptable to various scenarios.

Thanks for reaching out! We’re excited to engage with you and address any inquiries you may have about our products/services. Happy Testing till then :heart: