TestNG源码解读(八)

mac2025-02-10  19

6.3执行监听器-IExecutionListener*开始与结束

runExecutionListeners(true /* start */); runExecutionListeners(false /* finish */); //遍历listenersList如果start是true则执行onExecutionStart启动操作,否则执行finish操作 private void runExecutionListeners(boolean start) { for (List<IExecutionListener> listeners : Arrays.asList(m_executionListeners, m_configuration.getExecutionListeners())) { for (IExecutionListener l : listeners) { if (start) l.onExecutionStart(); else l.onExecutionFinish(); } } }

6.4 执行用例

//执行用例开始时间 m_start = System.currentTimeMillis(); // // Slave mode 肉鸡模式 // if (m_slavefileName != null) { SuiteSlave slave = new SuiteSlave( m_slavefileName, this ); slave.waitForSuites(); } // // Regular mode 常规模式 // else if (m_masterfileName == null) { suiteRunners = runSuitesLocally(); } // // Master mode master模式 // else { SuiteDispatcher dispatcher = new SuiteDispatcher(m_masterfileName); suiteRunners = dispatcher.dispatch(getConfiguration(), m_suites, getOutputDirectory(), getTestListeners()); } //执行用例结束时间 m_end = System.currentTimeMillis();

查看常规模式regular mode的执行过程:

1.首先就是判断suite的个数是否大于0,只有不为空的时候才会进行用例等一系列操作,否则报错"No test suite found. Nothing to run"

2.然后判断第一个suite的berbose的级别是否大于2,如果大于2则在控制台中输出

... ... TestNG 6.8.9beta by Cédric Beust (cedric@beust.com) ...

3. 第三步就是初始化suiteRunners,以确保没有配置问题

 

/** * This needs to be public for maven2, for now..At least * until an alternative mechanism is found. */ public List<ISuite> runSuitesLocally() { SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap(); if (m_suites.size() > 0) { if (m_suites.get(0).getVerbose() >= 2) { Version.displayBanner(); } // First initialize the suite runners to ensure there are no configuration issues. // Create a map with XmlSuite as key and corresponding SuiteRunner as value for (XmlSuite xmlSuite : m_suites) { createSuiteRunners(suiteRunnerMap, xmlSuite); } // // Run suites // if (m_suiteThreadPoolSize == 1 && !m_randomizeSuites) { // Single threaded and not randomized: run the suites in order for (XmlSuite xmlSuite : m_suites) { runSuitesSequentially(xmlSuite, suiteRunnerMap, getVerbose(xmlSuite), getDefaultSuiteName()); } } else { // Multithreaded: generate a dynamic graph that stores the suite hierarchy. This is then // used to run related suites in specific order. Parent suites are run only // once all the child suites have completed execution DynamicGraph<ISuite> suiteGraph = new DynamicGraph<ISuite>(); for (XmlSuite xmlSuite : m_suites) { populateSuiteGraph(suiteGraph, suiteRunnerMap, xmlSuite); } IThreadWorkerFactory<ISuite> factory = new SuiteWorkerFactory(suiteRunnerMap, 0 /* verbose hasn't been set yet */, getDefaultSuiteName()); GraphThreadPoolExecutor<ISuite> pooledExecutor = new GraphThreadPoolExecutor<ISuite>(suiteGraph, factory, m_suiteThreadPoolSize, m_suiteThreadPoolSize, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); Utils.log("TestNG", 2, "Starting executor for all suites"); // Run all suites in parallel pooledExecutor.run(); try { pooledExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); pooledExecutor.shutdownNow(); } catch (InterruptedException handled) { Thread.currentThread().interrupt(); error("Error waiting for concurrent executors to finish " + handled.getMessage()); } } } else { setStatus(HAS_NO_TEST); error("No test suite found. Nothing to run"); usage(); } // // Generate the suites report // return Lists.newArrayList(suiteRunnerMap.values()); }

 

最新回复(0)