无法运行多个类,以编程方式创建TestNG

时间:2017-11-18 04:15:53

标签: java xml testing testng

我创建了创建TestNG xml的程序并运行测试类&使用是/否

提及的测试用例

我创建了一个提到了类名的XLSX文件和带有是/否的测试用例来运行测试用例(请参考下面的屏幕)

Xlsx file with rows names , what testng xml have(test, classnames)

我创建了一个创建虚拟testng.xml文件的程序,并在xl中运行提到的类(请参考程序)

package generics;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;



public class ExecuteTestCase {

    static Logger logger = Logger.getLogger(ExecuteTestCase.class);

    public static void main(String[] ar) {

        PropertyFileReader fileReader = new PropertyFileReader();
        //String suite = fileReader.readPropFile("testSuite").trim();
        String testSetPath = fileReader.readPropFile("tsFilePath").trim();
        System.out.println(testSetPath);
        String testSetSheetName = fileReader.readPropFile("tsSheetName").trim();
        System.out.println(testSetSheetName);
        TestNG myTestNG = new TestNG();
        String suite = null;
        String test = null;
        try {
            List<Map> rows = ExcelReader.read(testSetPath, testSetSheetName);           
            System.out.println("Rows are::"+rows);
            for(Map map1:rows)
            {
                 suite=map1.get("TestClass").toString();
                 test=map1.get("Test").toString();
            }       
                     XmlSuite mySuite = new XmlSuite();
                     mySuite.setName(suite);                 
                     List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
                     List<XmlTest> myTests = new ArrayList<XmlTest>();
                     Map<String,String> testngParams = new HashMap<String,String> ();
                     XmlTest myTest = new XmlTest(mySuite);
                     myTest.setName(test);
                        List<XmlClass> myClasses = new ArrayList<XmlClass> ();
                         XmlClass cls = new XmlClass(suite);                         
                         List<XmlInclude> xmlInclude=new ArrayList<XmlInclude>();
                         myClasses.add(cls);

                     for(Map map:rows)
                     {
                         if((map.containsKey("TestClass") && map.containsKey("TestCaseName") && map.get("SelectYN").toString().equals("Y")))
                                 {                           
                                    //System.out.println("Adding suite:"+map.get("TestCaseName").toString());

                                    xmlInclude.add(new XmlInclude(map.get("TestCaseName").toString()));

                                 }
                     }


                    myTest.setXmlClasses(myClasses);
                     myTests.add(myTest);
                     mySuite.setTests(myTests);
                     mySuites.add(mySuite);
                     myTestNG.setXmlSuites(mySuites);
                     myTestNG.run();



        } catch (ClassNotFoundException e) {
            logger.error("ERROR: TestSuite: " + suite + "- Class not found..!!");
        } catch (Exception e) {
            logger.error("ERROR: Problem while reading test set" + testSetPath
                    + "..!!" + e.getMessage());
        }
    }
}

实际 - 即使我提到多个类名,它也只运行单个类,并在该类中编写测试用例。

预期 - 如何使用该类中编写的测试用例运行多个类。

0 个答案:

没有答案