使用TestNG的XmlSuite类动态生成testng.xml文件-创新互联
一、类和对应关系
网页题目:使用TestNG的XmlSuite类动态生成testng.xml文件-创新互联
本文路径:http://myzitong.com/article/dhijdo.html
- XmlSuite, 对应xml文件中的 suite 标签。
- XmlTest, 对应 test标签。
- XmlGroup, XmlClass, XmlPackage等等。
- setName(), 给定 suite 的名称。
- setConfigFailurePolicy(), 设置失败策略,比如continue。
- setParallel(), 设置同步执行策略,class,method等。
- setThreadCount(), 当同步执行时,大线程数。
- setParameters(), 入参为map,对应xml文件中的
标签。 - setTests(), 入参为XmlTest的list,对应
标签
XmlTest中含有group, package, classes
创新互联专注于三亚企业网站建设,响应式网站开发,商城系统网站开发。三亚网站建设公司,为三亚等地区提供建站服务。全流程按需网站策划,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务- setXmlSuite(), 需要给Test 指定Suite, 入参为XmlSuite
- addIncludedGroup(String group)
- setXmlPackages(List
) - setXmlClasses(List
)
初始化XmlClass,包含method, include, exclude
- setName(String name)
- setIncludedMethods(List
) - setExcludeMethods(List
m)
public class RunWithTestNgXml {
public static void main(String[] args) {
//Receive the arguments from command including groups, classes, package and so on
MapnodeValues = parseArgs(args);
ListxmlSuites = new ArrayList();
try {
//Could execute multiple suites
xmlSuites.add(getXmlSuite(nodeValues));
//Execute testng suites
TestNG testNG = new TestNG();
testNG.setXmlSuites(xmlSuites);
testNG.run();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//Generate XmlSuite
private static XmlSuite getXmlSuite() throws ClassNotFoundException {
XmlSuite suite = new XmlSuite();
suite.setName("Suites");
suite.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
//Set parallel as class
suite.setParallel(XmlSuite.ParallelMode.CLASSES);
//Set thread as 1
suite.setThreadCount(1);
//Set suite parameters
MapsuiteParams = generateSuiteMap();
suite.setParameters(suiteParams);
//Set test
ListxmlTests = getXmlTests(suite);
suite.setTests(xmlTests);
//if needed, also can write as testng.xml file
writeTestNgXml(suite);
return suite;
}
//Generate XmlTests
private static ListgetXmlTests(XmlSuite suite) {
ListxmlTestsList = new ArrayList();
XmlTest xmlTest = new XmlTest();
xmlTest.setName("Test");
xmlTest.setXmlSuite(xmlSuite);
//Set groups
Listgroups;
xmlTest.setIncludeGroups(groups);
//Set Packages
ListxmlPackages = new ArrayList();
xmlTest.setXmlPackages(xmlPackages);
//Set Classes
ListxmlClassList = new ArrayList();
xmlTest.setXmlClasses(xmlClassList);
xmlTestsList.add(xmlTest);
return xmlTestsList;
}
//Write testng.xml to disk
private static void writeTestNgXml(XmlSuite suite) {
String xmlStr = suite.toXml();
FileWriter writer = null;
try {
writer = new FileWriter( new File(C:/testng.xml"), false);
writer.write(xmlStr);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.flush();
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
四、执行使用command 执行main方法
cd {workspace}/build/libs/
java -cp {project}.jar com...RunWithTestNgXml groups=smoke,P0,test classes=${CLASSES} packages=${PACKAGES}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页题目:使用TestNG的XmlSuite类动态生成testng.xml文件-创新互联
本文路径:http://myzitong.com/article/dhijdo.html