Robot Framework-失败用例自动重跑(python3版本)

前言:

        近期在用Robot Framework框架实现网站核心用例的UI自动化测试,运行过程中,自动化测试脚本容易受环境影响等因素导致脚本运行失败,因此需要实现Robot Framework失败用例自动重跑3次的功能,减少脚本运行失败率;在网上找到了实现该功能的资料,但该资料是基于python2版本的,在实践过程中发现python3版本的配置与之存在一些差异,因此记录一下实践过程,以供参考。

正文:

        修改代码如下:

1、修改文件 C:\Python36\Lib\site-packages\robot\run.py

 1)在USAGE变量里添加下面红色内容。

Options

=======   

-y --retry retry    Set the retry times if test failed. 


2)增加导入模块 (python3已经默认编码utf-8,所以不用重新设置编码 )

from xml.dom import minidom


3)RobotFramework类增加make方法

注意事项:#savefile = open(outxml,'w',encoding='UTF-8')  ;open方法需要指定utf-8编码,否则在读取ouput.xml文件生成report.html会报编码错误。

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.items():

                for i in el[0:-1]:

                    textElement = i.nextSibling

                    suite.removeChild(i)

                    suite.removeChild(textElement)

        savefile = open(outxml,'w',encoding='UTF-8')

        root = xmldoc.documentElement

        root.writexml(savefile)

        savefile.close()


4)RobotFramework类的main方法

加入红色内容 self._make(settings.output)

2、robot/conf/settings.py

class RobotSettings(_BaseSettings) :修改_cli_opts字典,增加 'Retry':('retry',3)


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字符串,增加 -y --retry retry         Set the retry times if test failed.


5、在命令行或者ride运行校验失败的测试用例,正常运行并生成测试报告,则说明配置成功了。

备注:次数的配置在settings.py下retry后修改即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、文件robot\conf\settings.py 默认重试三次 在'RunEmptySuite' :...
    吴龙华Tester阅读 1,029评论 0 0
  • Robotframwork 自带的重试参数-R Note:out.html路径,同时要保证有log.html文件和...
    五娃儿阅读 1,833评论 1 1
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,554评论 0 13
  • 我愿化身石桥 历经四季轮回 相思桥上说相思 风雨厮守待回首 阿木对我说:我喜欢上了一女子。 我:是怎样的一个女子?...
    阿木石桥阅读 234评论 0 0
  • 一棵苹果树,终于结果了。 第一年,它结了10个苹果,9个被拿走,自己得到1个。对此,苹果树愤愤不平,于是自断经脉,...
    桔蓝_8650阅读 81评论 0 0