我们再来看 src/robot/run.py 的工作原理。摘录部分代码:

 from robot.conf import RobotSettings
from robot.model import ModelModifier
from robot.output import LOGGER, pyloggingconf
from robot.reporting import ResultWriter
from robot.running import TestSuiteBuilder
from robot.utils import Application, unic, text class RobotFramework(Application): def __init__(self):
Application.__init__(self, USAGE, arg_limits=(1,),
env_options='ROBOT_OPTIONS', logger=LOGGER) def main(self, datasources, **options):
...... def run_cli(arguments=None, exit=True):
if arguments is None:
arguments = sys.argv[1:]
return RobotFramework().execute_cli(arguments, exit=exit) def run(*tests, **options):
return RobotFramework().execute(*tests, **options) if __name__ == '__main__':
run_cli(sys.argv[1:])

在上一章我们提到Java的命令行入口其实最终还是转到了其它入口点,例如robot.run的run_cli(mytests.robot)

这里就先看第51行的run_cli方法 ,方法很简单,只是调用了RobotFramework类中的execute_cli方法。RobotFramework是run.py的一个内部类,也是Application的子类。通过第6行的 from robot.utils import Application可查看Application是做什么的。

src/robot/utils/application.py

摘录部分代码:

 class Application(object):

     def __init__(self, usage, name=None, version=None, arg_limits=None,
env_options=None, logger=None, **auto_options):
self._ap = ArgumentParser(usage, name, version, arg_limits,
self.validate, env_options, **auto_options)
self._logger = logger or DefaultLogger() def main(self, arguments, **options):
raise NotImplementedError ...... def execute_cli(self, cli_arguments, exit=True):
with self._logger:
self._logger.info('%s %s' % (self._ap.name, self._ap.version))
options, arguments = self._parse_arguments(cli_arguments)
rc = self._execute(arguments, options)
if exit:
self._exit(rc)
return rc
def _parse_arguments(self, cli_args):
try:
options, arguments = self.parse_arguments(cli_args)
except Information as msg:
self._report_info(msg.message)
except DataError as err:
self._report_error(err.message, help=True, exit=True)
else:
self._logger.info('Arguments: %s' % ','.join(arguments))
return options, arguments def parse_arguments(self, cli_args):
"""Public interface for parsing command line arguments. :param cli_args: Command line arguments as a list
:returns: options (dict), arguments (list)
:raises: :class:`~robot.errors.Information` when --help or --version used
:raises: :class:`~robot.errors.DataError` when parsing fails
"""
return self._ap.parse_args(cli_args) def execute(self, *arguments, **options):
with self._logger:
self._logger.info('%s %s' % (self._ap.name, self._ap.version))
return self._execute(list(arguments), options) def _execute(self, arguments, options):
try:
rc = self.main(arguments, **options)
except DataError as err:
return self._report_error(err.message, help=True)
except (KeyboardInterrupt, SystemExit):
return self._report_error('Execution stopped by user.',
rc=STOPPED_BY_USER)
except:
error, details = get_error_details(exclude_robot_traces=False)
return self._report_error('Unexpected error: %s' % error,
details, rc=FRAMEWORK_ERROR)
else:
return rc or 0

Application的execute_cli方法,其实也只是做了参数的解析工作(请看第17行 和 第18行的方法调用),具体的任务如何执行交给了本实例的main方法(第50行)。那么仍然回到 src/robot/run.py 看RobotFramework的main方法:

     def main(self, datasources, **options):
settings = RobotSettings(options)
LOGGER.register_console_logger(**settings.console_output_config)
LOGGER.info('Settings:\n%s' % unic(settings))
builder = TestSuiteBuilder(settings['SuiteNames'],
extension=settings.extension,
rpa=settings.rpa)
suite = builder.build(*datasources)
settings.rpa = builder.rpa
suite.configure(**settings.suite_config)
if settings.pre_run_modifiers:
suite.visit(ModelModifier(settings.pre_run_modifiers,
settings.run_empty_suite, LOGGER))
with pyloggingconf.robot_handler_enabled(settings.log_level):
old_max_error_lines = text.MAX_ERROR_LINES
text.MAX_ERROR_LINES = settings.max_error_lines
try:
result = suite.run(settings)
finally:
text.MAX_ERROR_LINES = old_max_error_lines
LOGGER.info("Tests execution ended. Statistics:\n%s"
% result.suite.stat_message)
if settings.log or settings.report or settings.xunit:
writer = ResultWriter(settings.output if settings.log
else result)
writer.write_results(settings.get_rebot_settings())
return result.return_code

在这个方法里,进行了设置项的赋值(第2行),真正执行测试并输出测试结果(第18行)。通过第5,8,18行可以看到测试的执行过程首先是通过TestSuiteBuilder构建了一个suite,然后执行该suite的run方法。那么我们来看看TestSuiteBuilder是如何构建一个suite的。

通过from robot.running import TestSuiteBuilder可以知道TestSuiteBuilder是在robot.running路径下,我们先看看这个包的__init__.py

 from .builder import TestSuiteBuilder, ResourceFileBuilder
from .context import EXECUTION_CONTEXTS
from .model import Keyword, TestCase, TestSuite
from .testlibraries import TestLibrary
from .usererrorhandler import UserErrorHandler
from .userkeyword import UserLibrary
from .runkwregister import RUN_KW_REGISTER

由第1行可以看出TestSuiteBuilder在 src/robot/running/builder.py:摘录的部分代码:

 class TestSuiteBuilder(object):

     def __init__(self, include_suites=None, warn_on_skipped='DEPRECATED',
extension=None, rpa=None):
self.include_suites = include_suites
self.extensions = self._get_extensions(extension)
builder = StepBuilder()
self._build_steps = builder.build_steps
self._build_step = builder.build_step
self.rpa = rpa
self._rpa_not_given = rpa is None
# TODO: Remove in RF 3.2.
if warn_on_skipped != 'DEPRECATED':
warnings.warn("Option 'warn_on_skipped' is deprecated and has no "
"effect.", DeprecationWarning)
...... def build(self, *paths):
"""
:param paths: Paths to test data files or directories.
:return: :class:`~robot.running.model.TestSuite` instance.
"""
if not paths:
raise DataError('One or more source paths required.')
if len(paths) == 1:
return self._parse_and_build(paths[0])
root = TestSuite()
for path in paths:
root.suites.append(self._parse_and_build(path))
root.rpa = self.rpa
return root
......

build方法的最后返回了一个TestSuite对象。走到这里好像有点太快了,为了更好的理解这个TestSuite,我们回过头来,顺藤摸瓜看看这个build的参数paths是什么:  def build(self, *paths)(builder.py) <-- builder.build(*datasources) (run.py)<-- def main(self, datasources, **options): <-- self.main(arguments, **options)(Application.py) <-- def _execute(self, arguments, options): <-- self._execute(arguments, options) <-- def execute_cli(self, cli_arguments, exit=True):(Application.py) <--RobotFramework().execute_cli(arguments, exit=exit)(run.py) <-- def run_cli(arguments=None, exit=True):(run.py)

原来这个paths是命令后选项参数或者是方法调用时传递过来的参数。例如

 from robot import run_cli

 # Run tests and return the return code.
rc = run_cli(['--name', 'Example', 'tests.robot'], exit=False) # Run tests and exit to the system automatically.
run_cli(['--name', 'Example', 'tests.robot'])

或者 像第一篇文章中 java -jar robotframework.jar run mytests.robot这个命令,经过JarRunner解析会最终调用robot.run的run_cli("mytests.robot")这个方法

所以这个TestSuiteBuilder的目的是通过解析datasource来构建一个TestSuite ,接着回到builder.py的 build方法最后的TestSuite对象上,来看看TestSuite什么。

通过robot.running的_init_.py :from .model import Keyword, TestCase, TestSuite,可以看出TestSuite在 src/robot/running/model.py,摘录有关TestSuite的代码:

 class TestSuite(model.TestSuite):
"""Represents a single executable test suite. See the base class for documentation of attributes not documented here.
"""
__slots__ = ['resource']
test_class = TestCase #: Internal usage only.
keyword_class = Keyword #: Internal usage only. def __init__(self, name='', doc='', metadata=None, source=None, rpa=False):
model.TestSuite.__init__(self, name, doc, metadata, source, rpa)
#: :class:`ResourceFile` instance containing imports, variables and
#: keywords the suite owns. When data is parsed from the file system,
#: this data comes from the same test case file that creates the suite.
self.resource = ResourceFile(source=source)
。。。。。。
def run(self, settings=None, **options):
from .namespace import IMPORTER
from .signalhandler import STOP_SIGNAL_MONITOR
from .runner import Runner with LOGGER:
if not settings:
settings = RobotSettings(options)
LOGGER.register_console_logger(**settings.console_output_config)
with pyloggingconf.robot_handler_enabled(settings.log_level):
with STOP_SIGNAL_MONITOR:
IMPORTER.reset()
output = Output(settings)
runner = Runner(output, settings)
self.visit(runner)
output.close(runner.result)
return runner.result

通过第10行的__init__方法可以看到,TestSuite初始化的时候包括name,doc,metadata,import,Variabel等数据。通过同一个图片我想大家应该就可以更 好的理解这里封装的信息了:

是的,就是这个可视化工具RIDE里的信息.当然这个类里面封装的信息并不全,因为它是model.TestSuite的子类,在父类中封装了更多的信息。

仍然回到  src/robot/run.py 的main方法,suite构建后会调用suite.run方法收集result。看 TestSuite类的第31行 self.visit(runner),这个visit方法都做了写什么?参数runner有时什么呢? 我们先在父类中看visit方法

/src/robot/model/testsuite.py

     def visit(self, visitor):
""":mod:`Visitor interface <robot.model.visitor>` entry-point."""
visitor.visit_suite(self)

方法很简单,只是去调用这个runner参数的visit_suite()方法。我们通过TestSuite类run方法中的from .runner import Runner可以知道 这个runner参数是:

src/robot/running/runner.py

 class Runner(SuiteVisitor):

 3 。。。。。。

Runner是SuiteVisitor的子类,里面并没有visit_suite 方法。去看看SuiteVisitor。 通过model包__init__.py的 from .visitor import SuiteVisitor 可知,SuiteVisitor在  /src/robot/model/visitor.py,摘录部分代码:

 class SuiteVisitor(object):
"""Abstract class to ease traversing through the test suite structure. See the :mod:`module level <robot.model.visitor>` documentation for more
information and an example.
""" def visit_suite(self, suite):
"""Implements traversing through the suite and its direct children. Can be overridden to allow modifying the passed in ``suite`` without
calling :func:`start_suite` or :func:`end_suite` nor visiting child
suites, tests or keywords (setup and teardown) at all.
"""
if self.start_suite(suite) is not False:
suite.keywords.visit(self)
suite.suites.visit(self)
suite.tests.visit(self)
self.end_suite(suite) def start_suite(self, suite):
"""Called when suite starts. Default implementation does nothing. Can return explicit ``False`` to stop visiting.
"""
pass def end_suite(self, suite):
"""Called when suite ends. Default implementation does nothing."""
pass

在visit_suite方法中,开始了测试的执行,start_suite,end_suite 都在Runner具体实现. 今天先写到这里,下一章再接着分析visit_suite()里调用的各个方法的具体实现.

如果喜欢作者的文章,请关注"写代码的猿"订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载.

Robot Framework源码解析(2) - 执行测试的入口点的更多相关文章

  1. Robot Framework 源码解析(1) - java入口点

    一直很好奇Robot Framework 是如何通过关键字驱动进行测试的,好奇它是如何支持那么多库的,好奇它是如何完成截图的.所以就打算研究一下它的源码. 这是官方给出的Robot framework ...

  2. MySQL源码解析之执行计划

    MySQL源码解析之执行计划 MySQL执行计划介绍 MySQL执行计划代码概览 MySQL执行计划总结 一.MySQL执行计划介绍 在MySQL中,执行计划的实现是基于JOIN和QEP_TAB这两个 ...

  3. Robot Framework 源码阅读 day1 run.py

    robot里面run起来的接口主要有两类 run_cli def run_cli(arguments): """Command line execution entry ...

  4. 【原创】angularjs1.3.0源码解析之执行流程

    Angular执行流程 前言 发现最近angularjs在我厂的应用变得很广泛,下周刚好也有个angular项目要着手开始做,所以先做了下功课,从源代码开始入手会更深刻点,可能讲的没那么细,侧重点在于 ...

  5. Robot Framework 源码阅读 day2 TestSuitBuilder

    接上一篇 day1 run.py 发现build test suit还挺复杂的, 先从官网API找到了一些资料,可以看出这是robotframework进行组织 测试案例实现的重要步骤, 将传入的te ...

  6. Robot Framework 源码阅读 day1 __main__.py

    robot文件夹下的__main__.py函数 是使用module运行时的入口函数: import sys # Allows running as a script. __name__ check n ...

  7. mongo源码学习(四)服务入口点ServiceEntryPoint

    在上一篇博客mongo源码学习(三)请求接收传输层中,稍微分析了一下TransportLayer的作用,这篇来看下ServiceEntryPoint是怎么做的. 首先ServiceEntryPoint ...

  8. Mybatis 系列10-结合源码解析mybatis 的执行流程

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

  9. Mybatis 系列4-结合源码解析节点:typeAliases

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...

随机推荐

  1. Python笔记之 - 一张截图诠释"文件读写" !

    Python笔记之 - 一张截图诠释"文件读写" ! 源代码如下: # 文件读写 str_test1 = "先创建txt文件再写入内容: 我是大帅哥" # wi ...

  2. windows SSH Tunnel实施日记

    1.准备条件:SSH跳板服务器一个.软件:Putty,CCProxy 2.putty建立SSH Tunnel:先在session那儿把服务器地址填好,到Tunnel界面上,选Dynamics和Auto ...

  3. 搭建centos7的开发环境2-单机版Hadoop2.7.3配置

    最近公司准备升级spark环境,主要原因是生产环境的spark和hadoop版本都比较低,但是具体升级到何种版本还不确定,需要做进一步的测试分析.这个任务对于大数据开发环境配置有要求,这里记录一下配置 ...

  4. require.js添加css文件实现代码:css.min.js

    define( function () { if (typeof window == "undefined")return { load: function (n, r, load ...

  5. 智能指针之 shared_ptr

     std::shared_ptr 是通过指针保持对象共享所有权的智能指针.多个 shared_ptr 对象可占有同一对象大概实现了一下,主要实现原理为,共享指针内部持有堆资源 的指针以及引用计数的指针 ...

  6. 微信小程序开发框架技术选型

    目前微信小程序开发有三种方式,原生微信小程序,使用mpVue或wepy微信小程序开发框架. 三种开发方式横向对比资料如下:  

  7. Python_自定义栈

    customStack.py '''栈:是一种运算受限的线性表,其特点在于仅允许在一端进行元素的插入和删除操作,最后入栈的最先出栈,而最先入栈的元素最后出栈''' s = [] s.append(3) ...

  8. Axios源码深度剖析 - 替代$.ajax,成为xhr的新霸主

    前戏 在正式开始axios讲解前,让我们先想想,如何对现有的$.ajax进行简单的封装,就可以直接使用原声Promise了? let axios = function(config){ return ...

  9. PAT1029:Median

    1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...

  10. java导入excel时处理日期格式(已验证ok)

    在Excel中的日期格式,比如2009-12-24将其转化为数字格式时变成了40171,在用java处理的时候,读取的也将是40171.如果使用POI处理Excel中的日期类型的单元格时,如果仅仅是判 ...