def beforeSubCaseStart(self, subCaseName): VAR.CurCase.CaseName = subCaseName VAR.CurCase.CaseResult = TestRunner.ResultPass VAR.CurCase.LoopPassTotal = TestRunner.LoopPassTotal VAR.CurCase.LoopFailTotal = TestRunner.LoopFailTotal VAR.CurCase.StartTime = time.strftime("%Y-%m-%d %H:%M:%S") LogPrint.addCaseLog()
def _inner(*args, **kwargs): if isPublish: pass params_str = getParams(args, kwargs, func) LogPrint.PRINTI('----->' + func.__name__ + '(%s)' % params_str) ret = func(*args, **kwargs) LogPrint.PRINTI('<-----' + func.__name__ + '(%s)' % str(ret)) return ret
def run(self, testCaseModule): try: scriptFilePath = '.'.join( [testCaseModule.__module__.replace('.', '/'), 'py']) LogPrint.PRINTI('Prepare to Start: %s' % scriptFilePath) if VAR.CurProject.IsHandleDevice == 'true': from autotest.core.conf import AnalyseConfig AnalyseConfig.collectTestDevice() testCase = testCaseModule() subTestCaseList = getattr(testCase, "TestCaseList") if not subTestCaseList: raise NoTestCaseException( 'has no testcase in TestCaseList, please check it.') except: LogPrint.PRINTTRAC() return # 让用户可以在脚本中以self.data.xx获取脚本配置文件中的数据 setattr(testCase, "data", VAR.CurCase.Config) # 开始子用例测试 for subTestCaseName in subTestCaseList: # 用例开始前准备工作 self.beforeSubCaseStart(subTestCaseName) # 检查用例是否存在且有效 subTestCase = self._checkSubTestCaseExist(testCase, subTestCaseName) if subTestCase is None: VAR.CurCase.CaseResult = TestRunner.ResultUnavailable else: # 获取用例循环次数,并开始执行用例 loopTimes = self._getSubCaseLoopTimes(subTestCaseName) for loopId in range(1, loopTimes + 1): self.printHeaderAndFooter(subTestCaseName, 'Loop_%s Start' % loopId) VAR.CurCase.CurLoopTime = loopId if self._runSetup(testCase, subTestCaseName): if self._runTest(subTestCase): VAR.CurCase.LoopPassTotal += 1 else: VAR.CurCase.CaseResult = TestRunner.ResultFail else: VAR.CurCase.CaseResult = TestRunner.ResultFail self._runTeardown(testCase, subTestCaseName) self.printHeaderAndFooter(subTestCaseName, 'Loop_%s End' % loopId) VAR.CurCase.EndTime = time.strftime("%Y-%m-%d %H:%M:%S") self._changeProjectVAR() # 将用例执行结果写入数据库 self._writeCaseResult() # 用例结束后的复位操作 self.afterSubCaseEnd(subTestCaseName)
def execute(self, sql): try: self.cursor.execute(sql) self.conn.commit() except: LogPrint.PRINTTRAC() self.conn.rollback()
def _run(self): VAR.CurProject.StartTime = time.strftime('%Y-%m-%d %H:%M:%S') # 开始记录工程执行日志 LogPrint.addProjectLog() if VAR.CurProject.RunMode == TestProject.FIXTURE_MODE: TestSuit().runFixtureMode() elif VAR.CurProject.RunMode == TestProject.RANDOM_MODE: TestSuit().runRandomMode() VAR.CurProject.EndTime = time.strftime('%Y-%m-%d %H:%M:%S') TestResultDB().addProjectResult() from autotest.core.output.Report import Report Report().output() printProjectSummary() # 结束记录工程执行日志 LogPrint.removeProjectLog() # 结束重置VAR信息 resetProjectVar()
def _checkSubTestCaseExist(self, testCase, subTestCaseName): try: self.printHeaderAndFooter(subTestCaseName, 'Start') subTestCase = getattr(testCase, subTestCaseName, None) if subTestCase is None: raise NoSuchCaseException('has no such case: %s' % subTestCaseName) except NoSuchCaseException: LogPrint.PRINTTRAC() return subTestCase
def _runTeardown(self, testCase, subTestCaseName): try: self.printHeaderAndFooter(subTestCaseName, 'Teardown Start') testCase.teardownStart() testCase.teardown() testCase.teardownEnd() self.printHeaderAndFooter(subTestCaseName, 'Teardown Passed') except: LogPrint.PRINTTRAC() self.printHeaderAndFooter(subTestCaseName, 'Teardown Failed')
def _runTest(self, subTestCase): try: self.printHeaderAndFooter(subTestCase.__name__, 'Test Start') subTestCase() self.printHeaderAndFooter(subTestCase.__name__, 'Test Passed') return True except: LogPrint.PRINTTRAC() self.printHeaderAndFooter(subTestCase.__name__, 'Test Failed') return False
def _runSetup(self, testCase, subTestCaseName): try: self.printHeaderAndFooter(subTestCaseName, 'Setup Start') testCase.setupStart() testCase.setup() testCase.setupEnd() self.printHeaderAndFooter(subTestCaseName, 'Setup Passed') return True except: LogPrint.PRINTTRAC() self.printHeaderAndFooter(subTestCaseName, 'Setup Failed') return False
def printProjectSummary(): endTime = time.mktime(time.strptime(VAR.CurProject.EndTime, '%Y-%m-%d %H:%M:%S')) startTime = time.mktime(time.strptime(VAR.CurProject.StartTime, '%Y-%m-%d %H:%M:%S')) s = '\n测试结束!\n运行时间: {time}min\n共计执行用例数量:{count}\n执行成功用例数量:{Pass}' \ '\n执行失败用例数量:{fail}\n无效用例数量:{unavailable}\n未执行用例数量:{notrun}\n' summary = s.format( time=round((endTime - startTime) / 60, 2), count=VAR.CurProject.Total, Pass=VAR.CurProject.Pass, fail=VAR.CurProject.Fail, unavailable=VAR.CurProject.Unavailable, notrun=VAR.CurProject.NotRun ) LogPrint.PRINTI(summary)
def TeardownStep(msg): LogPrint.PRINTI("环境恢复: " + str(msg))
def CheckPoint(msg): LogPrint.PRINTI("检测点: " + str(msg))
def TestStep(msg): LogPrint.PRINTI("测试步骤: " + str(msg))
def SetupStep(msg): LogPrint.PRINTI("预置条件: " + str(msg))
def afterSubCaseEnd(self, subCaseName): LogPrint.PRINTI('Test Case End: %s' % subCaseName) LogPrint.removeCaseLog() VAR.CurCase.CurLoopTime = 0 VAR.CurCase.CaseName = None VAR.CurCase.CaseResult = TestRunner.ResultNotRun
def printHeaderAndFooter(self, testCaseName, msg): LogPrint.PRINTI(''.join([self.PrintHeader, testCaseName, ': ', msg]))