如何将动态参数传递给 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>
我需要 运行 这个 class 多次,每次使用不同的 offerId 参数。 (例如 1234,4567,7899)
我只想 运行 这个请求一次,它会一次又一次地刺激所有不同的参数和 运行 whole suit,并且在同一份报告中给出结果。
这就是我所做的:
@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 sheet 中的每个值 运行。
为您提供有关如何将数据提供程序用于您的测试用例的示例。
@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 中参数化任何东西。您只需提及 class 名称,所有测试都会自动 运行。你 testng.xml 应该是这样的:
<test name="SampleTest">
<classes>
<class name="packageName.className" />
</classes>
</test>
以下代码的作用:
我想在运行时为每个参数添加一个参数列表。这些参数作为 Maven 运行时参数传递。使用 System.getProperty()
方法读取它们,如下所示。然后把这些参数加到<suite>
里面的<test>
,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();
我有 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>
我需要 运行 这个 class 多次,每次使用不同的 offerId 参数。 (例如 1234,4567,7899)
我只想 运行 这个请求一次,它会一次又一次地刺激所有不同的参数和 运行 whole suit,并且在同一份报告中给出结果。
这就是我所做的:
@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 sheet 中的每个值 运行。
为您提供有关如何将数据提供程序用于您的测试用例的示例。
@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 中参数化任何东西。您只需提及 class 名称,所有测试都会自动 运行。你 testng.xml 应该是这样的:
<test name="SampleTest">
<classes>
<class name="packageName.className" />
</classes>
</test>
以下代码的作用:
我想在运行时为每个参数添加一个参数列表。这些参数作为 Maven 运行时参数传递。使用 System.getProperty()
方法读取它们,如下所示。然后把这些参数加到<suite>
里面的<test>
,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();