使用自动化脚本进行测试,经常受环境影响等各方面导致本能成功的脚本失败,下面介绍了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.这一段

 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/).

增加导入模块

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

RobotFramework类增加make方法

 def make(self,outxml):
         xmldoc = minidom.parse(outxml)
         suiteElementList = xmldoc.getElementsByTagName('suite')
         mySuite = []
         for suiteElement in suiteElementList:
             if suiteElement.childNodes is not None:
                 for element in suiteElement.childNodes:
                     if element.nodeName == 'test':
                         mySuite.append(suiteElement)
                         break
         for suite in mySuite:
             testElements = {}
             for element in suite.childNodes:
                 if element.nodeName == 'test':
                     name = element.getAttribute('name')
                     if testElements.get(name) == None:
                         testElements.update({name:[element]})
                     else:
                         testElements.get(name).append(element)
             for n,el in testElements.iteritems():
                 for i in el[0:-1]:
                     textElement = i.nextSibling
                     suite.removeChild(i)
                     suite.removeChild(textElement)
         savefile = open(outxml,'w')
         root = xmldoc.documentElement
         root.writexml(savefile)
         savefile.close()

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

 def main(self, datasources, **options):
     settings = RobotSettings(options)
     LOGGER.register_console_logger(**settings.console_output_config)
     LOGGER.info('Settings:\n%s' % unic(settings))
     suite = TestSuiteBuilder(settings['SuiteNames'],
                              settings['WarnOnSkipped']).build(*datasources)
     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):
         result = suite.run(settings)
         LOGGER.info("Tests execution ended. Statistics:\n%s"
                     % result.suite.stat_message)
         self.make(settings.output)
         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、robot/conf/settings.py
修改_cli_opts字典,增加 'Retry':('retry',1)

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

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

 def visit(self, visitor):
         for item in self:
             if self.__module__ == 'robot.model.testcase' and hasattr(visitor,"_context"):
                 testStatus = ''
                 for i in range(0,int(visitor._settings._opts['Retry'])):
                     if testStatus != 'PASS':
                         if item.name in visitor._executed_tests:
                             visitor._executed_tests.pop(item.name)
                         item.visit(visitor)
                         testStatus = visitor._context.variables['${PREV_TEST_STATUS}']
                     else:
                         break
             else:
                 item.visit(visitor)

4、robotide\contrib\testrunner\usages.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/).
 

如果重跑次数达到设置上限,仍然失败,则报告显示为最后一次失败数据(结果)

【转载】扩展Robot Framework,实现失败用例自动再执行(失败重跑)的更多相关文章

  1. Robot Framework(十七) 扩展RobotFramework框架——扩展Robot Framework Jar

    4.4扩展Robot Framework Jar 使用标准JDK安装中包含的jar命令,可以非常简单地向Robot Framework jar添加其他测试库或支持代码.Python代码必须放在jar里 ...

  2. Robot Framework测试框架用例脚本设计方法

    Robot Framework介绍 Robot Framework是一个通用的关键字驱动自动化测试框架.测试用例以HTML,纯文本或TSV(制表符分隔的一系列值)文件存储.通过测试库中实现的关键字驱动 ...

  3. 使用远程接口库进一步扩展Robot Framework的测试能力

    引言: Robot Framework的四层结构已经极大的提高了它的扩展性.我们可以使用它丰富的扩展库来完成大部分测试工作.可是碰到下面两种情况,仅靠四层结构就不好使了: 1.有些复杂的测试可能跨越多 ...

  4. [Robot Framework] 如何在Setup中用Run Keywords执行多个带参数的关键字

    参考文档:http://www.howtobuildsoftware.com/index.php/how-do/bZ7q/robotframework-setup-teardown-robot-fra ...

  5. 基于RFS(robot framework selenium)框架模拟POST/GET请求执行自动化接口测试

    打开RIDE添加测试用例 如: Settings         Library Collections       Library RequestsLibrary       Test Cases ...

  6. robot framework 上个用例的输出作为下个用例的输入 (Set Global Variable的用法)

    变量的作用域 通常情况下,每个变量默认都是局部变量. 一个case里的变量,作用域在这个case内部: 一个userkeyword里的变量,作用域在这个userkeyword内部: 一个文件型suit ...

  7. TestNG失败用例自动截图

    参考:https://blog.csdn.net/wangxin1982314/article/details/50247245 1. 首先写一个截屏方法 public class ScreenSho ...

  8. [Robot Framework] Jenkins上调用Rebot命令时执行报错不往下执行其他命令

    在配置jenkins job时,添加构建步骤Execute Windows batch command,输入执行rebot命令 报错信息: Call C:\Python27\Scripts\rebot ...

  9. Robot Framework-失败用例自动重跑

    使用自动化脚本进行测试,经常受环境影响等各方面导致本能成功的脚本失败,下面介绍了RFS框架下,失败重跑的方法: 通过改写RobotFramework源代码增加–retry选项,实现test级别的失败用 ...

随机推荐

  1. JavaWeb知识回顾二

    动态web资源相关 1.tomcat相关 tomcat的目录结构 bin -- tomcat服务器的批处理文件的存放目录 conf -- tomcat服务器配置文件的存放目录 lib -- tomca ...

  2. 【python基础】 Tkinter小构件之canvas 画布

    [python之tkinter画布] 要画布就要使用tkinter的小构件,canvas(结构化的图形,用于绘制图形,创建图形编辑器以及实现自定制的小构件类) 我们先使用create_rectangl ...

  3. WinForm 制作一个简单的计算器

    namespace WindowsFormsApplication6 { public partial class Form1 : Form { //存储上次点击了什么按钮,0代表什么都没有点击,1代 ...

  4. java系列笔记---正则表达式(1)常用符号

    正则表达式---常用符号 首先声明,我这里列表的是经常使用的一些符号,如果你想得到全部,那建议你通过API中,搜索Pattern类,会得到所有符号. 字符类 [abc] a.b 或 c(简单类) [^ ...

  5. 使用python制作ArcGIS插件(3)ArcPy的使用说明

    使用python制作ArcGIS插件(3)ArcPy的使用说明 by 李远祥 ArcPy 是一个以成功的 arcgisscripting 模块为基础并继承了 arcgisscripting 功能进而构 ...

  6. PHP文本处理之中文汉字字符串转换为数组

    在PHP中我们可以通过str_split 将字符串转换为数组,但是却对中文无效,下面记录一下个人将中文字符串转换为数组的方法. 用到的PHP函数 mb_strlen - 获取字符串的长度 mb_sub ...

  7. Applovin Interview (面经)

    职位:SDE Intern positon 地点: San Jose 轮电面:self introduction what's your interest Concept of "Concu ...

  8. 分布式缓存技术memcached学习系列(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到"分布式一致性hash算法"这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前, ...

  9. React核心内容归纳总结

    状态.属性.组件API.组件的生命周期 当react的状态改变时,自动执行this.render()方法更新组件ES6写React的时候,事件里不会自动绑定this,需要自己绑定,或者直接在const ...

  10. jenkins全局安全设置

    如何进入安全设置界面          在Jenkins的主界面,点击 configure Global Security 选项,进入Jenkins的系统安全设置界面.安全界面如下图.在这里我们分别介 ...