本文主要是介绍TestNG的分析二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这章我们主要是自己构建testng的一些xmlclass,xmlsuite,xmltest,includemethod等对象来运行一个testng程序。
首先上一章testng的流测成图
相关代码:
public class test3 {public static void main(String[] args) {DynamicGraph<ISuite> suiteGraph = new DynamicGraph<ISuite>();SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap();XmlSuite xmlSuite = new XmlSuite();//xml test classXmlTest xmlTest = new XmlTest();xmlTest.setName("myTest");//xml test class,很多种初始化方法XmlClass xmlClass = new XmlClass("com.test.test2");// 上面类中 include的方法XmlInclude xmlIncludemethod = new XmlInclude("test");List<XmlInclude> methods = new ArrayList<>();methods.add(xmlIncludemethod);// set methods into xmlclassxmlClass.setIncludedMethods(methods);//xml class set into xml testList<XmlClass> classes = new ArrayList<>();classes.add(xmlClass);xmlTest.setClasses(classes);xmlTest.setSuite(xmlSuite); // 这个是必须的,不然在xmltest中会出错。这种设计有点相互联系,后面可以分析分析// xmlsuite: set testList<XmlTest> tests = new ArrayList<>();tests.add(xmlTest);xmlSuite.setTests(tests);//set package// XmlPackage xmlPackage = new XmlPackage();// xmlSuite.setPackages();System.out.println(xmlSuite);System.out.println(xmlSuite.getVerbose());createSuiteMap(suiteRunnerMap,xmlSuite);populateSuiteGraph(suiteGraph,suiteRunnerMap,xmlSuite);//将suiterunnermap加入graphIThreadWorkerFactory<ISuite> factory = new SuiteWorkerFactory(suiteRunnerMap,0 /* verbose hasn't been set yet */, "MyTestName");int corePoolSize = 1;GraphThreadPoolExecutor graphThreadPoolExecutor = new GraphThreadPoolExecutor(suiteGraph,factory,1,1,Integer.MAX_VALUE, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());graphThreadPoolExecutor.run();}// create suiteRunner 需要配置啥?public static void createSuiteMap(SuiteRunnerMap suiteRunnerMap,XmlSuite xmlSuite){SuiteRunner result = new SuiteRunner(getConfiguratio(),xmlSuite,"D:\\userdir\\");suiteRunnerMap.put(xmlSuite, result);}//copy from testNG()public static void populateSuiteGraph(DynamicGraph<ISuite> suiteGraph, SuiteRunnerMap suiteRunnerMap, XmlSuite xmlSuite){ISuite parentSuiteRunner = suiteRunnerMap.get(xmlSuite);if (xmlSuite.getChildSuites().isEmpty()) {suiteGraph.addNode(parentSuiteRunner);}else {for (XmlSuite childSuite : xmlSuite.getChildSuites()) {suiteGraph.addEdge(parentSuiteRunner, suiteRunnerMap.get(childSuite));populateSuiteGraph(suiteGraph, suiteRunnerMap, childSuite);}}}// copy from testNGpublic static IConfiguration getConfiguratio(){Configuration m_configuration = new Configuration();m_configuration.setAnnotationFinder(new JDK15AnnotationFinder(new DefaultAnnotationTransformer()));// m_configuration.setHookable(m_hookable);// m_configuration.setConfigurable(m_configurable);// m_configuration.setObjectFactory(factory);return m_configuration;}
}
其中需要注意的的几点:
- xmltest中有xmlsuite对象,需要设置。xmlsuite中有xmltest对象,也需要设置。感觉有点重复的感觉
- createSuiteMap是将xmlsuite与suiterunner 对应,runner也是在这个函数中创建的
- populateSuiteGraph 是将xmlsuite,xmlsuitemap, 加入到 一个graph中,这个graph维持着 testcase 的依赖关系和运行顺序,我们这里只设置了一个图中的一个node
- configuration是配置解析 代码中的 @ 这种标志的吧。
- 线程也是最简单的配置。factory继承线程类,是一个线程。
这篇关于TestNG的分析二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!