def getTCInfo(self, tcName): if not self.tcInfos.keys().__contains__(tcName): tcInfo = TValueGroup({}) tcInfo.Orders = None tcInfo.isTested = False self.tcInfos[tcName] = tcInfo return self.tcInfos[tcName]
def __init__(self): self.runModeEnum = {'slook':-22, 'scenario':-21, 'param':-11, 'look':-10, 'show':-9, 'debug':0, 'run':9, 'rerun':10, 'stop':-9} self.testResInfo = ['Passed', 'Warned', 'Failed', 'NotRun'] self.tlog = TestLoggerFactory() self.tprop = IniConfigure("mtest.ini") self.tassert = TestAssert(self.tlog) self.tcInfos = TValueGroup({}) self.init()
def addModelCls(self, modelClass, testModule, testName, imports, testOrder, searchKey): tcInfo = self.tc.getTCInfo(modelClass.__name__) for caseFun in dir(modelClass): if not MTConst.sysFunReg.isMatch(caseFun): testCaseInfo = TValueGroup({}) testCaseInfo.Name = caseFun tcInfo[caseFun] = {} tcInfo.Module = testModule tcInfo.Name = testName if testName != None else modelClass.__name__ tcInfo.Imports = imports tcInfo.Orders = testOrder tcInfo.SearchKey = searchKey
def __init__(self, tcObj, startStatus, strategy): self.startStatus = startStatus self.tlog = tcObj.tlog self.strategy = "product" if strategy == "all" else strategy self.isAcceptHandler = tcObj.__isAccept__ self.isContinueHandler = tcObj.__isContinue__ self.getStepArgHandler = tcObj.__getArgList__ self.isDebug = tcObj.__isDebug__() testSteps = TValueGroup({}) for sFunName in [ funName for funName in dir(tcObj) if not funName.startswith("__") ]: sFun = eval("tcObj.%s" % sFunName) if (hasattr(sFun, '__name__') and sFun.__name__ == 'stepDecorator'): sFunStatus = sFun() testSteps[sFunName] = { 'or': sFunStatus[0].split(','), 'to': sFunStatus[1], 'repeat': sFunStatus[2], 'cover': {} } self.testSteps = testSteps
def __init__(self, tParam, whereCondition, group): self.tParam = TValueGroup(tParam) self.condition = ObjOperation.tryGetVal(whereCondition, "condition", None) self.combSp = self.__combParam__(tParam, whereCondition, group) self.totalParams = self.combSp.getTotalNum() self.curOrderNum = 0 self.curParamIndex = 0
def __addPathToArgPathContainer(self, argPathContainer, testPath, repeatCount): pathParam = TValueGroup({}) stepIndexList = [] stepIndexToCombIndex = {} isCondCover = self.strategy == "condition" isEdgeCover = self.strategy == "edge" or self.strategy == "condEdge" or isCondCover notCondCover = self.strategy == "path" or self.strategy == "edge" or self.strategy == 'available' noNewEdge, stepIndex, combIndex = self.__prepareTestPath( testPath, pathParam, stepIndexList, stepIndexToCombIndex, isEdgeCover, isCondCover) if isEdgeCover and noNewEdge: return pathCovered = False stepArg = CombineSingleParam(pathParam, { 'combine': stepIndexList, 'strategy': self.strategy }, None) while True: pathArg = stepArg.nextParam() if pathArg == None: break acceptRes, acceptStepIndex = self.__isTestPathAccept( testPath, pathArg) if acceptRes <= 9: addTestPath = testPath if stepIndex > acceptStepIndex: addTestPath = list(testPath) delStepIndex = stepIndex while delStepIndex > acceptStepIndex: addTestPath.__delitem__(delStepIndex) pathArg.__delitem__(delStepIndex) delStepIndex -= 1 pathCovered = True argPathContainer.append({ 'path': addTestPath, 'accept': acceptRes, 'arg': pathArg }) if notCondCover: break addCombIndex = ObjOperation.tryGetVal(stepIndexToCombIndex, acceptStepIndex, combIndex) if addCombIndex < combIndex: stepArg.toNextParamArg(addCombIndex) if isEdgeCover and pathCovered: self.__coverTestPath(testPath)
def _getNextParam(self): if self.isList: strategyParam = TValueGroup({}) else: strategyParam = TValueGroup(self.tParam, True, isDeepClone=True) strategyParam.pIndex = self.curParamIndex strategyParam.paramName = None strategyParam.orderNum = self.curOrderNum self.__renderParam__(strategyParam) if self.isPureNum: self.curOrderNum = self.gcombSp.getNextPureOrderNum( self.curOrderNum) else: self.curOrderNum += 1 return strategyParam
class TestCaseFactory: def __init__(self): self.runModeEnum = {'slook':-22, 'scenario':-21, 'param':-11, 'look':-10, 'show':-9, 'debug':0, 'run':9, 'rerun':10, 'stop':-9} self.testResInfo = ['Passed', 'Warned', 'Failed', 'NotRun'] self.tlog = TestLoggerFactory() self.tprop = IniConfigure("mtest.ini") self.tassert = TestAssert(self.tlog) self.tcInfos = TValueGroup({}) self.init() def init(self, runMode='debug', tcPattern=None, outTcPattern=None, searchKey=None, logFilePath=None): try: self.runMode = self.runModeEnum[runMode] except: self.runMode = self.runModeEnum['debug'] self.searchKey = searchKey self.tcReg = PyRegExp(tcPattern) if tcPattern != None and tcPattern != "" else None self.tcRegOutScope = PyRegExp(outTcPattern) if outTcPattern != None and outTcPattern != "" else None self.isModeling = False logManager.removeHandler(slog, 1) logManager.addFileHandler(slog, None, "mtest.log") # set syslog for test if str(logFilePath).strip().endswith(".html"): from tmodel.model.logreport import HtmlTestReport self.tlog.registerLogger(HtmlTestReport, logFilePath, False) self.tlog.registerLogger(TestLogger, "testlog.log") else: self.tlog.registerLogger(TestLogger, logFilePath) def addRunMode(self, modeName, mode): self.runModeEnum[modeName] = mode def __checkSignal(self): if os.path.exists("stop.signal"): self.runMode = self.runModeEnum['stop'] else: while os.path.exists("pause.signal"): time.sleep(30) slog.info(".") def isInMode(self, modeName, modeType=0, isCheckSignal=False): if isCheckSignal and self.runMode >= self.runModeEnum['debug']: self.__checkSignal() try: modeCmp = self.runMode - self.runModeEnum[modeName] if modeType > 0: return modeCmp >= 0 elif modeType == 0: return modeCmp == 0 else: return modeCmp <= 0 except: return False def isInScope(self, csName, searchKeys=None): if self.searchKey != None and self.searchKey != "": if searchKeys == None: return False isNotMatch = True for searchVal in searchKeys.values(): if re.match(self.searchKey, searchVal) != None: isNotMatch = False break if isNotMatch: return False return (self.tcReg == None or self.tcReg.isMatch(csName)) and \ (self.tcRegOutScope == None or not self.tcRegOutScope.isMatch(csName)) def addResultType(self, resType, resInfo): while resType > 3: try: self.testResInfo[resType] = resInfo break except: self.testResInfo.append(None) def getResInfo(self, resType): return self.testResInfo[resType] def getTCInfo(self, tcName): if not self.tcInfos.keys().__contains__(tcName): tcInfo = TValueGroup({}) tcInfo.Orders = None tcInfo.isTested = False self.tcInfos[tcName] = tcInfo return self.tcInfos[tcName] def getTCDespcription(self, sparam, despFormat): if despFormat == "" or despFormat == None: if sparam.paramName == None: desp = "" else: desp = "(%s%s:%s)" % (("[%s]" % sparam.orderNum)if sparam.orderNum > 0 else "", sparam.paramName, sparam[sparam.paramName]) else: desp = despFormat for skey in sparam.keys(): stype = sparam.GetType(skey) if stype == None: stype = str(sparam[skey])[0:26] desp = desp.replace("{%s}" % skey, stype) return desp def getTCFullName(self, tcName, pIndex, desp): return "%s%s %s" % (tcName, pIndex if pIndex > 0 else "", desp) def getRunInfo(self, tcName, tempObj): tcInfo = self.getTCInfo(tcName) runList = [] if not tcInfo.isTested or self.runMode == 10: filteredList = [] for caseName in tcInfo.keys(): caseFun = eval("tempObj.%s" % caseName) if not hasattr(caseFun, '__name__') or 'ScenarioDecorator' != caseFun.__name__ : filteredList.append(caseName) for fName in filteredList: tcInfo.__delitem__(fName) runList = tcInfo.keys() if tcInfo.Orders != None: tOrders = type(tcInfo.Orders) if tOrders == bool: runList = list(runList) runList.sort(reverse=not tcInfo.Orders) if tOrders == list and len(tcInfo.Orders) > 0 and len(runList) > 1: runList = list(runList) orderList = [] for caseName in tcInfo.Orders: if runList.__contains__(caseName): orderList.append(caseName) runList.remove(caseName) runList = orderList + runList return (tcInfo, runList) def report(self, repHandler=slog.info, sumTitle='Test Summary:'): repRes = self.reportResult() if self.isInMode('show') or self.isInMode('scenario'): repHandler('\r\nFind Test Case(%s of %s):', repRes[MTConst.passed], repRes['num']) self.reportResult(MTConst.passed, repHandler) repHandler('Summary: %s of %s', repRes[MTConst.passed], repRes['num']) else: repHandler('\r\n%s\r\n%s' % (sumTitle, '-' * 60)) sumInfo = '\r\nTotal\t%s, %sm:\r\n\t' % (repRes['num'], int(repRes['time'] / 60 + 0.5)) hasRunRes = False for resType in range(4): try: resInfo = self.getResInfo(resType) resNum = ObjOperation.tryGetVal(repRes, resType, 0) if resType != MTConst.notRun: repHandler('%s\t%s:', resInfo, resNum) self.reportResult(resType, repHandler) sumInfo += (" + %s: %s" if hasRunRes else "%s: %s") % (resInfo, resNum) hasRunRes = True except Exception as ex: slog.warn(ex) continue repHandler(sumInfo) # repHandler('\r\nLog File:\r\n\t%s\n\n' % self.tlog.GetLoggerPath()) return repRes def reportResult(self, resType=None, repHandler=slog.info): runRep = {'num':0, 'time':0, MTConst.passed:0, MTConst.warned:0, MTConst.failed:0, MTConst.notRun:0} for tsName in self.tcInfos.keys(): tcInfo = self.getTCInfo(tsName) for tcName in tcInfo.keys(): tcResMap = tcInfo[tcName] for tpIndex in tcResMap.keys(): tcResInfo = tcResMap[tpIndex] tcRes = tcResInfo['r'] tcTime = tcResInfo['t'] runRep[tcRes] = ObjOperation.tryGetVal(runRep, tcRes, 0) + 1 runRep['num'] += 1 runRep['time'] += tcTime if resType == tcRes: repHandler("\t%s%s", self.getTCFullName(tcName, tpIndex, tcResInfo['d']) , ("\t%ss" % tcTime) if tcTime > 0 else "") return runRep
def diffJson(self, jsonStr, jsonStr2): return "%s\n%s" % ObjOperation.jsonEqual( TValueGroup(jsonStr).__prop__, TValueGroup(jsonStr2).__prop__)
def __init__(self): self.property = tprop self.tassert = tassert self.tlog = tlog self.param = TValueGroup({})
def testtvg(self): self.tlog.info(JsonParser().toStr({'a': TValueGroup({'b': 1})}))