美文网首页测试Inbox
robotframwork重试机制

robotframwork重试机制

作者: 五娃儿 | 来源:发表于2017-03-20 18:31 被阅读68次

    Robotframwork 自带的重试参数-R

    Note:out.html路径,同时要保证有log.html文件和report.html文件

    优点 :使用robotframwork自带参数,可以不用进行二次扩展

    缺点:需要自行整合报告,因为 重试后的报告,只是包含失败用的报告

    第二种方式:扩展源码

    通过改写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 xxxxxxx这一段

    Options

    =======

    -X--retry retry         xxxxxxx

    -N--name nameSetthe name of the top level test suite. Underscores

    inthe name are converted to spaces. Default nameis

    createdfromthe name of the executed data source.

    -D--doc documentationSetthe documentation of the top level test suite.

    Underscoresinthe documentation are converted to

    spacesandit may also contain simple HTML formatting

    (e.g.*bold*andhttp://url/).

    增加导入模块

    reload(sys)sys.setdefaultencoding('UTF-8')

    fromxml.domimportminidom

    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(width=settings['MonitorWidth'],

    colors=settings['MonitorColors'],

    markers=settings['MonitorMarkers'],

    stdout=settings['StdOut'],

    stderr=settings['StdErr'])

    LOGGER.info('Settings:\n%s' % unicode(settings))

    suite = TestSuiteBuilder(settings['SuiteNames'],

    settings['WarnOnSkipped'],

    settings['RunEmptySuite']).build(*datasources)

    suite.configure(**settings.suite_config)

    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 xxxxxxx这一段

    Options

    =======

    -X --retry retry         xxxxxxx

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

    优点: 不需要自行整合报告

    缺点:扩展源码,容易让“”小机器人“”罢工,建议反向扩展源码

    相关文章

      网友评论

        本文标题:robotframwork重试机制

        本文链接:https://www.haomeiwen.com/subject/mpdinttx.html