Multiple values for parameters in testng.xml (without using dataProvider)

arctic_monkey picture arctic_monkey · Jul 18, 2017 · Viewed 12.8k times · Source

I want to run my test case multiple times with different values of parameters. Is it possible using testng.xml and @Parameters annotation?

Eg.

 <test name="Login Tests">
    <parameter name="one" />
    <parameter name="two" />
    <classes>
        <class name="test.java.Login"/>
    </classes>
</test>

So, this should run the test two times, once with value one and then with value two.

Is it possible using testng.xml and @Parameter?

Q2. Also, is it possible to add parameters for only particular @Test in a suite

Eg. My TestSuite has 2 test cases and one testng.xml, associated to it.

Is it possible to add @Parameters in testng.xml for only one @Test, since both my tests are taking same parameters.

Answer

Krishnan Mahadevan picture Krishnan Mahadevan · Jul 19, 2017

The below sample should basically help answer all your questions.

How to run a @Test multiple times based on the values provided via the <parameters> tag

How to pass parameters to only a particular test class

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class FirstTestClass {

    @Test(dataProvider = "getData")
    public void testMethod(String param) {
        System.out.println("Name = " + param);
    }

    @DataProvider
    public Object[][] getData(ITestContext context) {
        String parameter = context.getCurrentXmlTest().getLocalParameters().get("names");
        String[] names = parameter.split(",");
        Object[][] returnValues = new Object[names.length][1];
        int index = 0;
        for (Object[] each : returnValues) {
            each[0] = names[index++].trim();
        }
        return returnValues;
    }
}

Here we are parsing a single parameter that was passed via the testng.xml file into multiple values by splitting them using ,

Here's how the second test class would look like, which is going to receive a test class specific parameter.

public class SecondTestClass {
    @Test
    @Parameters({"age"})
    public void testMethod(int age) {
        System.out.println("Age = " + age );
    }
}

Finally, here's how the testng.xml would look like :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45160355_Suite" parallel="false" verbose="2" >
    <test name="45160355_test" verbose="2">
        <parameter name="names" value="Cedric, Julien"/>
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn45160355.FirstTestClass">
            </class>
            <class name="com.rationaleemotions.stackoverflow.qn45160355.SecondTestClass">
                <parameter name="age" value="15"/>
            </class>
        </classes>
    </test>
</suite>

Here's the output

... TestNG 6.11 by Cédric Beust ([email protected])
...
{names=Cedric, Julien}
Name = Cedric
Name = Julien
Age = 15

===============================================
45160355_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================