解決RobotFramework用例執行失敗自動重跑問題
阿新 • • 發佈:2019-01-29
使用自動化指令碼進行測試,經常受環境影響等各方面導致本能成功的指令碼失敗,下面介紹了RFS框架下,失敗重跑的方法:
通過改寫RobotFramework原始碼增加--retry選項,實現test級別的失敗用例自動再執行:失敗用例會重跑N次,直至成功or 耗盡重試次數,生成的日誌和報告檔案中只會體現最後一次執行的結果。
修改程式碼如下:
一、修改檔案 C:\Python27\Lib\site-packages\robot\run.py
1)在USAGE變數裡新增下面紅色內容。
Options ======= -X --retry retry Set the retry times iftest 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 notNone: 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後修改即可。