[英]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.