TestNG - 以编程方式运行特定测试

时间:2016-04-19 06:03:29

标签: java testng

我正在尝试以编程方式创建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(){
    }

3 个答案:

答案 0 :(得分:4)

使用XmlInclude仅包含您想要的测试方法。

XmlClass xmlClass = new XmlClass("");
List<XmlInclude> includeMethods = new ArrayList<>();
includeMethods.add(new XmlInclude("test1"));
xmlClass.setIncludeMethods(includeMethods);

如果include方法列表未定义或定义为空,则TestNG将在类中运行所有测试。否则,它只会按其方法名称运行测试。

答案 1 :(得分:0)

您可以使用注释enabled = false

跳过测试
 @Test(testName="testCaseID" enabled="false")
 public void test1(){
    //code here..
 }

答案 2 :(得分:0)

下面的代码通过选择需要在特定类中执行的方法,可以完美地生成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();