zoukankan      html  css  js  c++  java
  • 【转载】扩展Robot Framework,实现失败用例自动再执行(失败重跑)

    使用自动化脚本进行测试,经常受环境影响等各方面导致本能成功的脚本失败,下面介绍了RFS框架下,失败重跑的方法:

    通过改写RobotFramework源代码增加--retry选项,实现test级别的失败用例自动再执行:失败用例会重跑N次,直至成功or 耗尽重试次数,生成的日志和报告文件中只会体现最后一次执行的结果。打个比方,用例A第一次执行失败了,立刻再重跑,再失败,立刻再重跑,成功了,那么,最后在生成的日志里面看到的就是最后那一次的运行数据,之前两次被完全过滤掉,只有在控制台中才可以看到它们的痕迹。

    eg:pybot.bat --retry 3 e: obot est

    retry设置为3,用例执行失败后会再次执行,每条用例最大执行次数为3成功后不会再执行。

    修改代码如下:

    1、robot/run.py

    修改USAGE字符串,增加 -X --retry retry         Set the retry times if test failed.这一段

    复制代码
     1 Options
     2 =======
     3  -X --retry retry      Set the retry times if test failed.
     4  -N --name name           Set the name of the top level test suite. Underscores
     5                           in the name are converted to spaces. Default name is
     6                           created from the name of the executed data source.
     7  -D --doc documentation   Set the documentation of the top level test suite.
     8                           Underscores in the documentation are converted to
     9                           spaces and it may also contain simple HTML formatting
    10                           (e.g. *bold* and http://url/).
    复制代码

    增加导入模块

    1 reload(sys)
    2 sys.setdefaultencoding('UTF-8')
    3 from xml.dom import minidom

    RobotFramework类增加make方法

    复制代码
     1 def make(self,outxml):
     2         xmldoc = minidom.parse(outxml)
     3         suiteElementList = xmldoc.getElementsByTagName('suite')
     4         mySuite = []
     5         for suiteElement in suiteElementList:
     6             if suiteElement.childNodes is not None:
     7                 for element in suiteElement.childNodes:
     8                     if element.nodeName == 'test':
     9                         mySuite.append(suiteElement)
    10                         break
    11         for suite in mySuite:
    12             testElements = {}
    13             for element in suite.childNodes:
    14                 if element.nodeName == 'test':
    15                     name = element.getAttribute('name')
    16                     if testElements.get(name) == None:
    17                         testElements.update({name:[element]})
    18                     else:
    19                         testElements.get(name).append(element)
    20             for n,el in testElements.iteritems():
    21                 for i in el[0:-1]:
    22                     textElement = i.nextSibling
    23                     suite.removeChild(i)
    24                     suite.removeChild(textElement)
    25         savefile = open(outxml,'w')
    26         root = xmldoc.documentElement
    27         root.writexml(savefile)
    28         savefile.close()
    复制代码

    修改RobotFramework类的main方法,插入self.make(settings.output)这段(被我编辑了一下,关键字没高亮了,不过没关系)

    复制代码
     1 def main(self, datasources, **options):
     2     settings = RobotSettings(options)
     3     LOGGER.register_console_logger(**settings.console_output_config)
     4     LOGGER.info('Settings:
    %s' % unic(settings))
     5     suite = TestSuiteBuilder(settings['SuiteNames'],
     6                              settings['WarnOnSkipped']).build(*datasources)
     7     suite.configure(**settings.suite_config)
     8     if settings.pre_run_modifiers:
     9         suite.visit(ModelModifier(settings.pre_run_modifiers,
    10                                   settings.run_empty_suite, LOGGER))
    11     with pyloggingconf.robot_handler_enabled(settings.log_level):
    12         result = suite.run(settings)
    13         LOGGER.info("Tests execution ended. Statistics:
    %s"
    14                     % result.suite.stat_message)
    15         self.make(settings.output)            
    16         if settings.log or settings.report or settings.xunit:
    17             writer = ResultWriter(settings.output if settings.log
    18                                   else result)
    19             writer.write_results(settings.get_rebot_settings())
    20     return result.return_code
    复制代码

    2、robot/conf/settings.py
    修改_cli_opts字典,增加 'Retry':('retry',1)

    1 'MonitorColors'    : ('monitorcolors', 'AUTO'),
    2                  'StdOut'           : ('stdout', None),
    3                  'StdErr'           : ('stderr', None),
    4                  'XUnitSkipNonCritical' : ('xunitskipnoncritical', False),
    5                   'Retry':('retry',1)}

    3、robot/model/itemlist.py
    修改visit方法如下

    复制代码
     1 def visit(self, visitor):
     2         for item in self:
     3             if self.__module__ == 'robot.model.testcase' and hasattr(visitor,"_context"):
     4                 testStatus = ''
     5                 for i in range(0,int(visitor._settings._opts['Retry'])):
     6                     if testStatus != 'PASS':
     7                         if item.name in visitor._executed_tests:
     8                             visitor._executed_tests.pop(item.name)
     9                         item.visit(visitor)
    10                         testStatus = visitor._context.variables['${PREV_TEST_STATUS}']
    11                     else:
    12                         break
    13             else:
    14                 item.visit(visitor)
    复制代码

    4、robotidecontrib estrunnerusages.py
    修改USAGE字符串,增加 -X --retry retry         Set the retry times if test failed.这一段

    复制代码
    Options
    =======
    -X --retry retry      Set the retry times if test failed.
    -N --name name Set the name of the top level test suite. Underscores in the name are converted to spaces. Default name is created from the name of the executed data source.
    -D --doc documentation Set the documentation of the top level test suite. Underscores in the documentation are converted to spaces and it may also contain simple HTML formatting (e.g. *bold* and http://url/).
     
  • 相关阅读:
    flex datagrid进行删除或增加操作后自动刷新
    java 一个很简单的applet
    转:35岁前必成功的12级跳(男女通用)
    flex DisplayObject UIComponent的区别
    flex flash.utils.Dictionary和Object
    java 正则表达式进行剔除字符
    Flex 元数据标签使用
    使用TableAdapter的Update方法使用注意事项
    android中捕捉menu按键的点击事件
    Repeater 嵌套repeater输出不规则列表
  • 原文地址:https://www.cnblogs.com/lgqboke/p/8194729.html
Copyright © 2011-2022 走看看