class MTConst: beginTestCaseInfo = "\t%s Running ..." endTestCaseInfo = "%s:\t%s" equalInfo = "Assert %sEqual, in fact %sEqual: Expected \"%s\", Actual \"%s\" for \"%s\"" conditionInfo = "Assert %s, in fact %s for %s" containInfo = "Contains %s for %s: %s should be contained in: %s" jsonContainInfo = "JSON %scontained, for %s:\n%s" sysFunReg = PyRegExp('__.+__|BeginTestCase|EndTestCase|SetUp|TearDown|Property|Assert|Logger') exceptReg = PyRegExp(".*driver[a-z|A-Z|]*.py|.*casemodel[a-z|A-Z|]*.py") passed = 0 warned = 1 failed = 2 notRun = 3
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 setRunCase(self, tcPattern=None, outTcPattern=None, searchKey=None, runMode=None, isclearresult=False): self.tcReg = PyRegExp( tcPattern) if tcPattern != None and tcPattern != "" else None self.tcRegOutScope = PyRegExp( outTcPattern ) if outTcPattern != None and outTcPattern != "" else None self.searchKey = None if searchKey is None or searchKey == "" else searchKey.replace( ",", " ").split() if runMode is not None: try: self.runMode = self.runModeEnum[runMode] except: self.runMode = self.runModeEnum['debug'] if isclearresult: self.tcInfos.clear()
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
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() self.tassert = TestAssert(self.tlog) self.tcInfos = {} self.startTime = time.time() self.init() def init(self, runMode="debug", testrunConfig="", logFilePath="", tcPattern=None, outTcPattern=None, searchKey=None, propConf={}): if not self.tprop.load(testrunConfig): if testrunConfig != "": slog.warn("Not found testrunConfig: %s" % testrunConfig) for sec in propConf: if not self.tprop.sections.__contains__(sec): self.tprop.sections[sec] = propConf[sec] else: for p in propConf[sec]: self.tprop.sections[sec][p] = propConf[sec][p] self.isModeling = False self.setRunCase(tcPattern, outTcPattern, searchKey, runMode) self.setRunLog(logFilePath) def setRunLog(self, logs): logManager.removeHandler(slog, 1) logManager.addFileHandler(slog, None, "mtest.log") # set syslog for test self.tlog.clearLogger() if len(logs) == 0: logs = ['console'] for l in logs: if l.endswith(".html"): from tmodel.model.logreport import HtmlTestReport self.tlog.registerLogger(HtmlTestReport, l, False) else: self.tlog.registerLogger(TestLogger, l, l.endswith(".xml")) def setRunCase(self, tcPattern=None, outTcPattern=None, searchKey=None, runMode=None, isclearresult=False): self.tcReg = PyRegExp( tcPattern) if tcPattern != None and tcPattern != "" else None self.tcRegOutScope = PyRegExp( outTcPattern ) if outTcPattern != None and outTcPattern != "" else None self.searchKey = None if searchKey is None or searchKey == "" else searchKey.replace( ",", " ").split() if runMode is not None: try: self.runMode = self.runModeEnum[runMode] except: self.runMode = self.runModeEnum['debug'] if isclearresult: self.tcInfos.clear() 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 is not None: if searchKeys is None: return False v = searchKeys.values() for k in self.searchKey: if not v.__contains__(k): return False return (self.tcReg is None or self.tcReg.isMatch(csName)) and \ (self.tcRegOutScope is 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 getCaseInfo(self, tsInfo, pIndex, desp, searchKey): if tsInfo.__contains__(pIndex): return tsInfo[pIndex] caseInfo = {'d': desp, 'r': MTConst.notRun, 't': 0, 'k': searchKey} tsInfo[pIndex] = caseInfo return caseInfo def getRunInfo(self, tcName, tempObj): tcInfo = self.getTCInfo(tcName) runList = [] if not tcInfo.isTested or self.runMode == 10: filteredList = [] for caseName in tcInfo.keys(): try: caseFun = eval("tempObj.%s" % caseName) except: raise Exception("Deuplicate class %s, no method %s" % (tempObj.__class__, 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 mergeResult(self, tcInfos, tstatuses): for tsName in tcInfos.keys(): atcInfo, tcInfo = tcInfos[tsName], self.tcInfos[tsName] for tcName in atcInfo.keys(): atcResMap, tcResMap = atcInfo[tcName], tcInfo[tcName] for tpIndex in atcResMap.keys(): if tstatuses.__contains__(atcResMap[tpIndex]['r']): tcResMap[tpIndex] = atcResMap[tpIndex] 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 = { 'startTime': self.startTime, 'endTime': time.time(), 'num': 0, 'time': 0, 'cases': { MTConst.passed: {}, MTConst.warned: {}, MTConst.failed: {}, MTConst.notRun: {} }, 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 tcFullName = self.getTCFullName(tcName, tpIndex, tcResInfo['d']) if resType is None: searchkey = tcResInfo['k'] author = ObjOperation.tryGetVal( searchkey, 'author', '') priority = ObjOperation.tryGetVal( searchkey, 'priority', '') runRep['cases'][tcRes][tcFullName] = "%ss%s%s" % ( tcTime, "" if author == "" else (", %s" % author), "" if priority == "" else (", %s" % priority)) elif resType == tcRes: repHandler("\t%s%s", tcFullName, ("\t%ss" % tcTime) if tcTime > 0 else "") return runRep