改写RobotFramework源代码增加--retry选项,实现test级别的失败用例自动再执行:失败用例会重跑N次,直至成功or 耗尽重试次数,生成的日志和报告文件中只会体现最后一次执行的结果。
修改代码如下:
一、修改文件 C:\Python27\Lib\site-packages\robot\run.py
1)在USAGE变量里添加下面红色内容。
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.
2)增加导入模块
reload(sys)
sys.setdefaultencoding('UTF-8')
from xml.dom import minidom
3)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()
4)RobotFramework类的main方法,加入红色内容 self._make(settings.output)
二、robot/conf/settings.py
修改_cli_opts字典,增加 'Retry':('retry',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)
四、robotide\contrib\testrunner\usages.py
修改USAGE字符串,增加 -X --retry retry Set the retry times if test failed.
现在完成了所有配置,可以运行一下试试了。次数的配置在settings.py下retry后修改即可。
备注:本文转自网上,经实践如果原原本本按照文中步骤的话会报错“robot.errors.FrameworkError: Option '-X' multiple times in usage”,把步骤一“ -X --retry retry Set the retry times if test failed.”删掉即可
在run.py的main方法,加入红色内容 self._make(settings.output)后系统会报错
[ ERROR ] Unexpected error: AttributeError: 'RobotFramework' object has no attribute '_make'
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 83, in _execute
rc = self.main(arguments, **options)
File "C:\Python27\lib\site-packages\robot\run.py", line 450, in main
self._make(settings.output)
导致无法查看报告,这时候可以把self._make(settings.output)改成self.make(settings.output)就能解决问题