51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2188|回复: 2
打印 上一主题 下一主题

[Robot] 扩展Robot Framework,实现失败用例自动再执行(失败重跑)

[复制链接]
  • TA的每日心情
    擦汗
    3 天前
  • 签到天数: 1021 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2017-4-27 13:37:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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


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

    eg:pybot.bat --retry 3 e:\robot\test

    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:\n%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:\n%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、robotide\contrib\testrunner\usages.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
    5. of the top level test suite. Underscores
    6. in the name are converted to spaces. Default name
    7. is created from the name
    8. of the executed data source. -D --doc documentation Set the documentation
    9. of the top level test suite. Underscores
    10. in the documentation are converted to spaces
    11. and it may also contain simple HTML formatting (e.g. *bold*
    12. and http://url/).






    13. 如果重跑次数达到设置上限,仍然失败,则报告显示为最后一次失败数据
    复制代码


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏2
    回复

    使用道具 举报

  • TA的每日心情

    2024-7-8 09:00
  • 签到天数: 943 天

    连续签到: 1 天

    [LV.10]测试总司令

    2#
    发表于 2017-4-27 15:05:26 | 只看该作者
    哈哈 管理又来分享了
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-22 01:24 , Processed in 0.073619 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表