09. Passing Parameters Through testng.xml and @Parameters annotation

Passing Parameters Through testng.xml and @Parameters annotation
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.

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 Others;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class parameter_TestNG {
    /**
     * 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);
    }
}

Testng.xml:

<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="Others.parameter_TestNG">
                <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="Others.parameter_TestNG">
                <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="Others.parameter_TestNG">
                <methods>
                    <include name="prameterTestThree" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

Output:
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

===============================================

No comments:

Post a Comment