如何将动态参数传递给 testNG.xml 运行多个测试 [英] How to pass dynamic parameter TO testNG.xml run multiple tests

查看:37
本文介绍了如何将动态参数传递给 testNG.xml 运行多个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有发送多个测试和多个参数的 xml 套件.

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

示例:

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

我需要多次运行这个类,每次使用不同的 offerId 参数.(例如 1234,4567,7899)

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.

这就是我所做的:

@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();

}

所以这将加载并运行我需要的套装,但如何更改套装内的参数?(之后我将创建 for 循环)

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)

[目前我复制了 xml 并手动更改了每个测试的参数.然后运行套件套件]

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

测试:

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

推荐答案

以下代码的作用:我想在运行时为每个参数添加一个参数列表.这些参数作为 maven 运行时参数传递.它们使用 System.getProperty() 方法读取,如下所示.然后将这些参数添加到里面的中,testng就运行成功了.这在其他场景中也非常有用.

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 <test> inside <suite> and testng is ran successfully. This can be really useful in other scenarios as well.

下面的代码读取testng.xml文件并添加参数

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();

这篇关于如何将动态参数传递给 testNG.xml 运行多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆