[英]TestNG - running specific tests programmatically
我正在尝试以编程方式创建 testng.xml。 我使用下面的Java代码
public static createTestSuit(String testClass){
XmlSuite suite = new XmlSuite();
suite.setName("My Suite");
XmlTest test = new XmlTest(suite);
test.setName("My Test");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass(testClass));
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
“testClass”类包含几个测试方法。 我不想运行所有这些方法。 如何仅指定我想在上面的代码中运行的测试方法名称,以便上面的方法看起来像
public static createTestSuit(String testClass, List<String> testCasesID){
//code
}
注:我的测试方法是形式
@Test(testName="testCaseID")
public void test1(){
}
使用XmlInclude
仅包含您想要的那些测试方法。
XmlClass xmlClass = new XmlClass("");
List<XmlInclude> includeMethods = new ArrayList<>();
includeMethods.add(new XmlInclude("test1"));
xmlClass.setIncludedMethods(includeMethods);
如果包含方法列表未定义或定义为空,则 TestNG 将运行类中的所有测试。 否则,它将仅运行按方法名称包含的测试。
您可以使用 annotation enabled=false 跳过测试
@Test(testName="testCaseID" enabled="false")
public void test1(){
//code here..
}
下面的代码通过选择需要在特定类中执行的方法来完美地生成 TestNG xml。
//Creating TestNG object
TestNG myTestNG = new TestNG();
//Creating XML Suite
XmlSuite mySuite = new XmlSuite();
//Setting the name for XML Suite
mySuite.setName("My Suite");
//Setting the XML Suite Parallel execution mode as Methods
mySuite.setParallel(XmlSuite.ParallelMode.METHODS);
//Adding the Listener class to the XML Suite
mySuite.addListener("test.Listener1");
//Creating XML Test and add the Test to the Suite
XmlTest myTest = new XmlTest(mySuite);
//Setting the name for XML Test
myTest.setName("My Test");
//Setting the Preserve Order for XML Test to True to execute the Test in Order
myTest.setPreserveOrder("true");
//Creating HashMap for setting the Parameters for the XML Test
HashMap<String,String> testngParams = new HashMap<String,String> ();
testngParams.put("browserName", "Chrome");
testngParams.put("browserVersion","81");
myTest.setParameters(testngParams);
//Creating XML Class
XmlClass myClass = new XmlClass("test.MainTest1");
//Creating XML Include in the form of ArrayList to add Multiple Methods which i need to run from the Class
List<XmlInclude> myMethods = new ArrayList<>();
myMethods.add(new XmlInclude("method1"));
myMethods.add(new XmlInclude("method2"));
//Adding the Methods selected to the my XML Class defined
myClass.setIncludedMethods(myMethods);
//Getting the Classes and adding it to the XML Test defined
myTest.getClasses().add(myClass);
//Creating XML Suite in the form of ArrayList and adding the list of Suites defined
List<XmlSuite> mySuitesList = new ArrayList<XmlSuite>();
mySuitesList.add(mySuite);
//Adding the XMLSuites selected to the TestNG defined
myTestNG.setXmlSuites(mySuitesList);
//Setting the execution Thread Count for Parallel Execution
mySuite.setThreadCount(10);
//Setting the Verbose Count for Console Logs
mySuite.setVerbose(2);
//Executing the TestNG created
myTestNG.run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.