gpt4 book ai didi

java - 使用 Java 以编程方式创建 TestNg Xml 文件

转载 作者:行者123 更新时间:2023-12-01 16:39:17 25 4
gpt4 key购买 nike

我有一个以下 testNg xml 文件。

有人可以建议如何使用 java 动态创建它吗?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<groups>
<run>
<include name="PrometheusHome" />
<include name="AlertMgrHome" />
</run>
</groups>
<classes>
<class name="com.amex.eag.telemetry.testcases.PrometheusTests"/>
<class name="com.amex.eag.telemetry.testcases.AlertManagerTests"/>
</classes>
</test>
</suite>

最佳答案

here: 调整核心

 public class DynamicTestNG {
public void runTestNGTest(Map<String,String> testngParams)
{ //Create an instance on TestNG
TestNG myTestNG = new TestNG();

//Create an instance of XML Suite and assign a name for it.
XmlSuite mySuite = new XmlSuite();
mySuite.setName("Suite");
mySuite.setParallel(XmlSuite.ParallelMode.METHODS);

//Create an instance of XmlTest and assign a name for it.
XmlTest myTest = new XmlTest(mySuite);
myTest.setName("Test");
//add groups
myTest.addIncludedGroup("PrometheusHome")
myTest.addIncludedGroup("AlertMgrHome")

//Add any parameters that you want to set to the Test.
myTest.setParameters(testngParams);

//Create a list which can contain the classes that you want to run.
List<XmlClass> myClasses = new ArrayList<XmlClass>();
myClasses.add(new XmlClass("com.amex.eag.telemetry.testcases.PrometheusTests"));
myClasses.add(new XmlClass("com.amex.eag.telemetry.testcases.AlertManagerTests"));

//Assign that to the XmlTest Object created earlier.
myTest.setXmlClasses(myClasses);

//Create a list of XmlTests and add the Xmltest you created earlier to it.
List<XmlTest> myTests = new ArrayList<XmlTest>();
myTests.add(myTest);

//add the list of tests to your Suite.
mySuite.setTests(myTests);

//Add the suite to the list of suites.
List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
mySuites.add(mySuite);

//Set the list of Suites to the testNG object you created earlier.
myTestNG.setXmlSuites(mySuites);
mySuite.setFileName("myTemp.xml");
mySuite.setThreadCount(3);
myTestNG.run();

//Create physical XML file based on the virtual XML content
for(XmlSuite suite : mySuites)
{
createXmlFile(suite);
}
System.out.println("File generated successfully.");

//Print the parameter values
Map<String,String> params = myTest.getParameters();
for(Map.Entry<String, String> entry : params.entrySet())
{
System.out.println(entry.getKey() + " => " + entry.getValue());
}
}
//This method will create an Xml file based on the XmlSuite data
public void createXmlFile(XmlSuite mSuite)
{
FileWriter writer;
try {
writer = new FileWriter(new File("myTemp.xml"));
writer.write(mSuite.toXml());
writer.flush();
writer.close();
System.out.println(new File("myTemp.xml").getAbsolutePath());
} catch (IOException e)
{
e.printStackTrace();
}
}
//Main Method
public static void main (String args[])
{
DynamicTestNG dt = new DynamicTestNG();
//This Map can hold your testng Parameters.
Map<String,String> testngParams = new HashMap<String,String> ();
testngParams.put("device1", "Desktop");
dt.runTestNGTest(testngParams);
}
}

关于java - 使用 Java 以编程方式创建 TestNg Xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61898211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com