如何使用robotframework的ExecutionResult

前面在分析参数设置的时候,我利用robotframework的参数解析机制实现了一个自己的提取失败测试用例的脚本,其实robotframework已经实现了自己的失败用例提取的功能, 那么分析完robotframework的失败用例提取的原理之后是否也可以加以利用了。

其实在提取失败用例的时候,最关键也是最开始的部分就是获得一个ExecutionResult对象,最后可以通过visitor来处理或者也可以直接访问result的内容:visitor的模式,这里已经有了, 就不多说了。

下面介绍下直接使用获得的ExecutionResult对象,我用它将所有的keywords名字给打印出来

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Thomas创建于:2015/6/5.
#


"""
通过例子来使用ExecutionResult
"""

from robot.result import ExecutionResult

# 只拿结果中suite来用
suite = ExecutionResult("D:/output/output-20150605-165314.xml").suite

# suite.suites是一个iterator对象,无法直接index访问,尝试__getitem__打印输出
print type(suite.suites)
print type(suite.suites.__getitem__(0))
print suite.suites.__getitem__(0).name
print suite.suites.__getitem__(0).full_message
print suite.suites.__getitem__(0).tests
print suite.suites.__getitem__(0).suites.__getitem__(0).suites.__getitem__(0).keywords
print suite.name
print suite.full_message
print suite.tests
print '-'*30

# 通过递归将所有的keywords打印出来
def print_all_keyword(suite):
    for sub_suite in suite.suites:
        print_all_keyword(sub_suite)
    if suite.keywords:
        print suite.keywords
    for test in suite.tests:
        print test.keywords

print_all_keyword(suite)

下面有一个极佳的使用实例, 比较output.xml的状态不同的脚本,官网提供的robotdiff.py, 这里就用到了ExecutionResult

#!/usr/bin/env python

#  Copyright 2008-2013 Nokia Solutions and Networks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""Diff Tool for Robot Framework Outputs

Usage:  robotdiff.py [options] input_files

This script compares two or more Robot Framework output files and creates a
report where possible differences between test case statuses in each file
are highlighted. Main use case is verifying that results from executing same
test cases in different environments are same. For example, it is possible to
test that new Robot Framework version does not affect test results. Another
usage is comparing earlier test results with newer ones to find out possible
status changes and added test cases.

Options:
 -r --report file         HTML report file (created from the input files).
                          Default is 'robotdiff.html'.
 -n --name name *         Custom names for test runs. If this option is used,
                          it must be used as many times as there are input
                          files. By default test run names are got from the
                          input file names.
 -t --title title         Title for the generated diff report. The default
                          title is 'Test Run Diff Report'.
 -E --escape what:with *  Escape certain characters which are problematic in
                          console. 'what' is the name of the character to
                          escape and 'with' is the string to escape it with.
                          Available character to escape:
                          <--------------------ESCAPES------------------------>
                          Example:
                          --escape space:_ --title My_Fine_Diff_Report
 -h -? --help             Print this usage instruction.

Options that can be specified multiple times are marked with an asterisk (*).

Examples:
$ robotdiff.py output1.xml output2.xml output3.xml
$ robotdiff.py --name Env1 --name Env2 smoke1.xml smoke2.xml
"""

import sys
import os.path

from robot.utils import ArgumentParser, NormalizedDict, HtmlWriter
from robot.result import ExecutionResult
from robot.errors import DataError, Information


def main(args):
    opts, paths = _process_args(args)
    results = DiffResults()
    for path, name in zip(paths, _get_names(opts['name'], paths)):
        try:
            results.add_output(path, name)
        except DataError, err:
            _exit(err, error=True)
    reporter = DiffReporter(opts['report'], opts['title'])
    reporter.report(results)
    _exit('Report: %s' % reporter.outpath)

def _process_args(cliargs):
    ap = ArgumentParser(__doc__, arg_limits=(2, ))
    try:
        return ap.parse_args(cliargs)
    except Information, msg:
        _exit(msg)
    except DataError, err:
        _exit(err, error=True)

def _get_names(names, paths):
    if not names:
        return [None] * len(paths)
    if len(names) == len(paths):
        return names
    _exit('Different number of test run names (%d) and input files (%d).'
          % (len(names), len(paths)), error=True)

def _exit(msg, error=False):
    print unicode(msg)
    if error:
        print "\nTry --help for usage information."
    sys.exit(int(error))


class DiffResults(object):

    def __init__(self):
        self._stats = NormalizedDict()
        self.column_names = []

    @property
    def rows(self):
        return (RowStatus(name, statuses)
                for name, statuses in sorted(self._stats.items()))

    def add_output(self, path, column=None):
        self._add_suite(ExecutionResult(path).suite)
        self.column_names.append(column or path)
        for stats in self._stats.values():
            self._add_missing_statuses(stats)

    def _add_suite(self, suite):
        self._add_to_stats(suite)
        for sub_suite in suite.suites:
            self._add_suite(sub_suite)
        for test in suite.tests:
            self._add_to_stats(test)

    def _add_to_stats(self, item):
        stats = self._stats.setdefault(item.longname, [])
        self._add_missing_statuses(stats)
        stats.append(ItemStatus(item))

    def _add_missing_statuses(self, stats):
        while len(stats) < len(self.column_names):
            stats.append(MissingStatus())


class MissingStatus(object):
    name = 'N/A'
    status = 'not_available'


class ItemStatus(object):

    def __init__(self, item):
        self.name = item.status
        self.status = item.status.lower()


class RowStatus(object):

    def __init__(self, name, statuses):
        self.name = name
        self._statuses = statuses

    @property
    def status(self):
        passed = any(stat.name == 'PASS' for stat in self)
        failed = any(stat.name == 'FAIL' for stat in self)
        missing = any(stat.name == 'N/A' for stat in self)
        if passed and failed:
            return 'diff'
        if missing:
            return 'missing'
        return 'all_passed' if passed else 'all_failed'

    @property
    def explanation(self):
        return {'all_passed': 'All passed',
                'all_failed': 'All failed',
                'missing': 'Missing items',
                'diff': 'Different statuses'}[self.status]

    def __iter__(self):
        return iter(self._statuses)


class DiffReporter(object):

    def __init__(self, outpath=None, title=None):
        self.outpath = os.path.abspath(outpath or 'robotdiff.html')
        self._title = title or 'Test Run Diff Report'
        self._writer = HtmlWriter(open(self.outpath, 'w'))

    def report(self, results):
        self._start(results.column_names)
        for row in results.rows:
            self._write_row(row)
        self._end()

    def _start(self, columns):
        self._writer.content(START_HTML % {'TITLE': self._title}, escape=False)
        self._writer.start('tr')
        self._writer.element('th', 'Name', {'class': 'col_name'})
        for name in columns:
            self._writer.element('th', name, {'class': 'col_status'})
        self._writer.end('tr')

    def _write_row(self, row):
        self._writer.start('tr')
        self._write_name(row)
        for item in row:
            self._write_status(item)
        self._writer.end('tr')

    def _write_name(self, row):
        self._writer.element('td', row.name, {'class': 'col_name ' + row.status,
                                              'title': row.explanation})

    def _write_status(self, item):
        self._writer.element('td', item.name,
                             {'class': 'col_status ' + item.status})

    def _end(self):
        for tag in 'table', 'body', 'html':
            self._writer.end(tag)
        self._writer.close()


START_HTML = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Expires" content="Mon, 20 Jan 2001 20:01:21 GMT">
<style media="all" type="text/css">
  body {
    background: white;
    font-family: sans-serif;
    font-size: 0.8em;
    color: black;
  }
  table {
    border: 1px solid black;
    border-collapse: collapse;
    empty-cells: show;
    margin: 0px 1px;
  }
  th, td {
    border: 1px solid black;
  }
  th {
    background: #C6C6C6;
  }
  .col_name {
    min-width: 25em;
    font-weight: bold;
  }
  .col_status {
    min-width: 6em;
    text-align: center;
  }
  .pass {
    color: #0F0;
  }
  .fail {
    color: #F00;
  }
  .not_available {
    color: #777;
  }
  .all_passed, .all_failed {
    background: #0F0;
  }
  .missing {
    background: #FF0;
  }
  .diff {
    background: #F00;
  }
</style>
<title>%(TITLE)s</title>
</head>
<body>
<h1>%(TITLE)s</h1>
<table>
'''[1:]


if __name__ == '__main__':
    main(sys.argv[1:])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Robot Framework,你需要按照以下步骤进行安装和配置: 1. 安装Robot Framework和相关库:可以使用pip命令来安装Robot Framework及其它需要的库。例如,可以通过运行以下命令来安装SeleniumLibrary、Requests和AppiumLibrary: ``` pip install robotframework-seleniumlibrary==3.0.0 pip install robotframework-requests pip install robotframework-appiumlibrary ``` 2. 配置标准库位置:安装完Robot Framework后,你需要将标准库的位置添加到你的Python环境中。在PyCharm中,标准库一般位于`E:\python\Lib\site-packages\robot\libraries`目录中。你可以将这个路径添加到你的Python环境变量中,以便Robot Framework可以找到标准库。 3. 配置Python环境:在使用Robot Framework之前,你需要确保已经安装了Python,并且将Python的安装路径添加到环境变量中。你可以从Python官方网站下载并安装Python。安装完成后,打开一个管理员身份的命令提示符窗口,并执行以下操作: - 首先,安装Robot Framework。如果安装出错,可以先卸载旧版本的Robot Framework,然后重新安装。可以通过运行以下命令来安装Robot Framework 3.1: ``` pip uninstall robotframework pip install robotframework==3.1 ``` - 其次,安装RIDE(Robot Framework的开发工具)。RIDE提供了一个用户友好的界面来编辑和运行Robot Framework的测试用例。可以通过运行以下命令来安装RIDE: ``` pip install robotframework-ride ``` 注意,该命令可能会下载比较慢,你可以使用豆瓣源来进行下载: ``` pip install -i https://pypi.douban.com/simple robotframework-ride ``` 完成以上步骤后,你就可以开始使用Robot Framework来编写和执行自动化测试了。你可以参考官方文档和教程来学习更多关于Robot Framework的用法和功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [RobotFramework简介及使用](https://blog.csdn.net/weixin_45043349/article/details/121086432)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值