How to pass dynamic parameter TO testNG.xml run multiple tests

瘦欲@ 提交于 2020-04-18 07:26:20

问题


I have xml suite that sends multiple tests and multiple parameters.

example:

        <test name="Create">       
        <classes>       
        <class name="TestClass">
            <methods>
                <parameter name="offerId" value="1234"/>
                <include name="testmethod"/>
            </methods>
        </class>                                          
      </classes>
      </test>
        <test name="Add">       
        <classes>       
        <class name="TestClass2">
            <methods>
                <include name="testmethod2"/>
            </methods>
        </class>                                          
      </classes>
      </test>

I need to run this class multiple times, each time with different offerId parameter. (e.g 1234,4567,7899)

I want to run this request only once, and it will irritate over all different parameter and run the whole suit again and again, and give result on the same report.

this is what I did:

@Test
public void runSuites2(){

    TestNG testng = new TestNG();
    List<String> suites=new ArrayList<String>();
    suites.add("c:/tests/testng1.xml");//path to xml..

    testng.setTestSuites(suites);
    testng.run();

}

so this will load and run the suit I need, but how to change the parameter inside the suite? (after it I will create for loop)

[currently I duplicated the xml and manually change the parameter for each test. and then run suite-of-suites]

the test:

@Parameters({ "offerId" })
@Test
public void testmethod(String offerId, ITestContext context) throws Exception {
    Reporter.log("offer ID is = " + offerId, true);
        }

回答1:


In this case, you can use dataprovider or you can read the values from excel and tests will be run for each value in the dataprovider/excel sheet.
Providing you an example on how to use dataprovider for your test case.

@DataProvider(name = "offerId")
public static Object[][] voiceSearchTestData() {
    return new Object[][]{
            {1234},
            {2345},
            {4567}
    };
}

@Test(dataProvider = "offerId")
public void testmethod(int offerId, ITestContext context) throws Exception {
    Reporter.log("offer ID is = " + offerId, true);
}

So the above test will run 3 times, one for each value present in the dataprovider and you dont need to parameterise anything in the testng xml. You just need to mention the class name and all the tests will run automatically. You testng.xml should be like:

<test name="SampleTest">
    <classes>
        <class name="packageName.className" />
    </classes>
</test>



回答2:


I know it's a bit late for this answer. But it might help others so I am posting it. Please upvote if you like my answer.

What the below code does: I want to add a list of parameters to each during runtime. These parameters are passed as maven runtime arguments. They are read using System.getProperty() method as shown below. Then these parameters are added to the inside and testng is ran successfully. This can be really useful in other scenarios as well.

The below code reads the testng.xml file and adds parameter to

List<String> parameters = new ArrayList<>();
parameters = Arrays.asList(System.getProperty("parameters").split(",");

TestNG tng = new TestNG();
File initialFile = new File("testng.xml");
InputStream inputStream = FileUtils.openInputStream(initialFile);
Parser p = new Parser(inputStream);
List<XmlSuite> suites = p.parseToList();
for(XmlSuite suite:suites){
    List<XmlTest> tests = suite.getTests();
    for (XmlTest test : tests) {
         for (int i = 0; i < parameters.size(); i++) {
            HashMap<String, String> parametersMap = new HashMap<>();
            parametersMap.put("parameter",parameters.get(i));
            test.setParameters(parametersMap);
        }
    }
}
tng.setXmlSuites(suites);
tng.run();


来源:https://stackoverflow.com/questions/54556925/how-to-pass-dynamic-parameter-to-testng-xml-run-multiple-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!