testng的分析一

2024-04-19 14:18
文章标签 分析 testng

本文主要是介绍testng的分析一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

运行的代码是 自己设置listener, test class 设置是.class, 看看这个是testng run的路线

	TestListenerAdapter testListenerAdapter = new TestListenerAdapter();TestNG testNG = new TestNG();testNG.setTestClasses(new Class[]{test2.class});testNG.addListener(testListenerAdapter);testNG.run();List<ITestResult> testresults =testListenerAdapter.getPassedTests();System.out.println(testresults.get(0).getStatus());

testNG.ru()
then we will enter TestNG 的方法,进入 这个方法之前,我们已经将testclass传入了testng对像,但是未设置testsuite。
问题,testclass什么时候会被testng使用,并且会传给什么对象去处理。

public void run() {initializeSuitesAndJarFile();initializeConfiguration();initializeDefaultListeners();initializeCommandLineSuites();initializeCommandLineSuitesParams();initializeCommandLineSuitesGroups();sanityCheck();List<ISuite> suiteRunners = null;runExecutionListeners(true /* start */);//// Regular mode//else if (m_masterfileName == null) {suiteRunners = runSuitesLocally();}//// Master mode//  

initializeSuitesAndJarFile() 主要是设置 m_jatPath ,m_suites.。 在我们的例子中,没有suite,也未设置jar path , 所以两个都是0.
initializeConfiguration()

  • Install the listeners found in ServiceLoader (or use the class
    loader for tests, if specified).
  • Install the listeners found in the suites
  • Install the method selectors ???
  • Find if we have an object factory
    下面是设置一些其他的
m_configuration.setAnnotationFinder(new JDK15AnnotationFinder(getAnnotationTransformer()));
m_configuration.setHookable(m_hookable);
m_configuration.setConfigurable(m_configurable);
m_configuration.setObjectFactory(factory);

接下来是进入runseiteslocally, 我们知道之前返回的m_suite是空的,至少在initialsuiteandjar时候是空的。

 public List<ISuite> runSuitesLocally() {SuiteRunnerMap suiteRunnerMap = new SuiteRunnerMap();if (m_suites.size() > 0) {if (m_suites.get(0).getVerbose() >= 2) {Version.displayBanner();}

运行到这里,我发现这个m_suites并不为空,说明前面那个步骤必然是将test.class加入了testsuite中。我们发现,执行到 initializeCommandLineSuitesParams();这时候 suite就不为0了,可见suute的初始化在这个函数中做掉了。

private void initializeCommandLineSuites() {if (m_commandLineTestClasses != null || m_commandLineMethods != null) {if (null != m_commandLineMethods) {m_cmdlineSuites = createCommandLineSuitesForMethods(m_commandLineMethods);}else {m_cmdlineSuites = createCommandLineSuitesForClasses(m_commandLineTestClasses);}for (XmlSuite s : m_cmdlineSuites) {for (XmlTest t : s.getTests()) {t.setPreserveOrder(m_preserveOrder ? "true " : "false");}m_suites.add(s);if (m_groupByInstances != null) {s.setGroupByInstances(m_groupByInstances);}}}}

发现,m_commandLineTestClasses 就是 com.test.test2, 因此会仅需执行,这里我们的m_commandLineMethods 是为null的,
所以最终会调用,
m_cmdlineSuites = createCommandLineSuitesForClasses(m_commandLineTestClasses);
接着会被m_suites加入到list中。

有了suite之后,我们再回到runsuitelocally()
加下来就是创建线程去run这些worker.

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 valuefor (XmlSuite xmlSuite : m_suites) {createSuiteRunners(suiteRunnerMap, xmlSuite);}//// Run suites//if (m_suiteThreadPoolSize == 1 && !m_randomizeSuites) {// Single threaded and not randomized: run the suites in orderfor (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 executionDynamicGraph<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 parallelpooledExecutor.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());}}}e//// Generate the suites report//return Lists.newArrayList(suiteRunnerMap.values());}

这篇关于testng的分析一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/917779

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb