robotframework--ExecutionResult

366 阅读3分钟

背景

我正在参加「掘金·启航计划」 好久不写文章了,前一阵有个掘友私信我,让我写一写robot framework api 中executionresult模块的用法,之前使用robot framework时也浅浅用过, 正好借这个机会我分享下

介绍

学习这方面的知识,我喜欢看官方文档,这个ExecutionResult也让我们先去官方文档看一下, 官方文档里写着这样一句话,ExecutionResult是一个工厂方法(什么是工厂方法,下次我们再研究),这个方法解析xml output file

image.png 这个xml out file,熟悉RF的知道它是执行case会输出测试报告文件里的其中一个,这个文件包含有关测试执行的所有信息( contains all the information about test execution),ExecutionResult通过解析读取这个xml,就能够拿到你需要的执行信息,进行自己的操作

我们再来看下这个方法的源码,更进一步了解, 它有两个参数,sources我们可以传入你要解析的xml文件, options参数是一些配置,例如当merge设为true时,如果有多个执行结果就会合并,后面的结果覆盖前面的; 该方法返回的是 class:~.executionresult.Result的一个实例

def ExecutionResult(*sources, **options):
    """Factory method to constructs :class:`~.executionresult.Result` objects.

    :param sources: XML source(s) containing execution results.
        Can be specified as paths, opened file objects, or strings/bytes
        containing XML directly. Support for bytes is new in RF 3.2.
    :param options: Configuration options.
        Using ``merge=True`` causes multiple results to be combined so that
        tests in the latter results replace the ones in the original.
        Setting ``rpa`` either to ``True`` (RPA mode) or ``False`` (test
        automation) sets execution mode explicitly. By default it is got
        from processed output files and conflicting modes cause an error.
        Other options are passed directly to the
        :class:`ExecutionResultBuilder` object used internally.
    :returns: :class:`~.executionresult.Result` instance.

    Should be imported by external code via the :mod:`robot.api` package.
    See the :mod:`robot.result` package for a usage example.
    """
    if not sources:
        raise DataError('One or more data source needed.')
    if options.pop('merge', False):
        return _merge_results(sources[0], sources[1:], options)
    if len(sources) > 1:
        return _combine_results(sources, options)
    return _single_result(sources[0], options)

这个 class:~.executionresult.Result可在源码文件executionresult.py查看

image.png

使用举例

让我们举例说明

例子1,检查执行时间

这是官方文档上的例子,当case执行的时间大于设定的预期时间,这个case我们就认为执行失败,并给出提示信息“执行时间太长了”,然后输出结果,如果输出结果的文件没有指定,就会重写原来的输入文件 我们可以把代码拷贝到本地,传入参数执行 我先手工执行一些case,这个case2我让它执行超过10秒,生成执行报告

image.png

报告,生成了三个文件

image.png

查看原始结果

image.png

我们执行检查执行时间的例子,我让其执行时间大于3秒就是失败

image.png

执行完之后,再次打开报告文件(output.xml会更新),看测试结果有无变化, 按照预期发生变化

image.png

例子2, 我用来分析执行结果传到另外一平台进行展示

直接看我的源码,我是先通过robot的run方法执行完所有的用例,生成结果,然后调用ExecutionResult方法分析每个case的状态,保存,传入另一平台展示, 文件头要引用 from robot.api import ExecutionResult

image.png

总结

最主要还是要熟读源码,结合实践使用,有问题欢迎交流