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

相关文章

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java