簡體   English   中英

如何將動態參數傳遞給 testNG.xml 運行多個測試

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

我有發送多個測試和多個參數的 xml 套件。

例子:

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

我只想運行這個請求一次,它會刺激所有不同的參數並一次又一次地運行整個套裝,並在同一個報告上給出結果。

這就是我所做的:

@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 循環)

[目前我復制了 xml 並手動更改了每個測試的參數。 然后運行套件套件]

考試:

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

在這種情況下,您可以使用 dataprovider 或者您可以從 excel 中讀取值,並且將為 dataprovider/excel 表中的每個值運行測試。
為您提供有關如何為您的測試用例使用 dataprovider 的示例。

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

所以上面的測試將運行 3 次,數據提供程序中存在的每個值一次,您不需要在 testng xml 中參數化任何內容。 您只需要提及類名,所有測試都會自動運行。 你 testng.xml 應該是這樣的:

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

以下代碼的作用:我想在運行時向每個參數添加一個參數列表。 這些參數作為 maven 運行時參數傳遞。 它們使用System.getProperty()方法讀取,如下所示。 然后將這些參數添加到<suite>里面的<test> <suite> ,testng就運行成功了。 這在其他場景中也非常有用。

下面的代碼讀取 testng.xml 文件並將參數添加到

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM