def doSuiteCase(self): try: globalVars.getLogger().info("开始执行案例") self.cases = self.suite.getCases() for i in self.cases: globalVars.getLogger().info(" ") globalVars.getLogger().info("案例名称:"+ CommonValueHandle.text2str(i["name"])) te = TestCaseExcute(i["cId"],self.globalVars,self.runningVars,[],self.task.env,0,self.task.nextResultVersion) re = te.excute() reId = -1 if True != re: globalVars.getLogger().info("执行案例出现错误!"+CommonValueHandle.text2str(re)) reId = te.saveResult(self.task.id,"执行案例出现错误!"+CommonValueHandle.text2str(re)) self.failed= self.failed+1 else: reId = te.saveResult(self.task.id,"执行成功") # self.resultIds.append(reId) if te.result > -1: self.success= self.success+1 else: self.failed= self.failed+1 self.resultIds.append(reId) except Exception as e: globalVars.getLogger().err("案例执行失败:"+CommonValueHandle.text2str(e.message)) return CommonValueHandle.text2str(e.message) else: return True
def changeCommonDataTurn(self, Commonfield, direction, commonId): if not getattr(self, Commonfield) and getattr(self, Commonfield) != "": return Commonfield + ":没有这样的字段" com = getattr(self, Commonfield) jsonData = [] if com == "" or com == None: jsonData = [] else: jsonData = json.loads(getattr(self, Commonfield)) if isinstance(jsonData, list): commonId = int(commonId) for i, v in enumerate(jsonData): if v == commonId: if direction == "up": if i != 0: jsonData[i] = jsonData[i - 1] jsonData[i - 1] = commonId else: if i != (len(jsonData) - 1): jsonData[i] = jsonData[i + 1] jsonData[i + 1] = v try: setattr(self, Commonfield, json.dumps(jsonData)) self.save() except Exception as e: globalVars.getLogger().error( Commonfield + "删除失败" + CommonValueHandle.text2str(e.message)) return Commonfield + "删除header失败:" + CommonValueHandle.text2str( e.message) else: return commonId else: globalVars.getLogger().error(Commonfield + "的数据格式不正确") return Commonfield + "的数据格式不正确"
def delCommonData(self, Commonfield, CommonId): if not getattr(self, Commonfield) and getattr(self, Commonfield) != "": return Commonfield + ":没有这样的字段" com = getattr(self, Commonfield) jsonData = [] if com == "" or com == None: jsonData = [] else: jsonData = json.loads(getattr(self, Commonfield)) if isinstance(jsonData, list): try: CommonId = int(CommonId) for i in jsonData: if i == CommonId: CommonParmasModel.objects.get(pk=CommonId).delete() jsonData.remove(i) setattr(self, Commonfield, json.dumps(jsonData)) self.save() except Exception as e: globalVars.getLogger().error( Commonfield + "删除失败" + CommonValueHandle.text2str(e.message)) return Commonfield + "删除header失败:" + CommonValueHandle.text2str( e.message) else: return CommonId else: globalVars.getLogger().error(Commonfield + "的数据格式不正确") return Commonfield + "的数据格式不正确"
def excute(self): ret = self.getTask() if(True != ret): self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,获取任务失败") globalVars.getLogger().error("获取任务失败"+ret) return "获取任务失败"+ret if(not self.getSuite()): self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,获取测试套件失败") globalVars.getLogger().error("获取测试套件失败") return "获取测试套件失败" ret = self.initGlobalVars() if isinstance(ret,str) or isinstance(ret,unicode): self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,初始化全局变量失败") globalVars.getLogger().error("初始化全局变量失败:"+CommonValueHandle.text2str(ret)) return "初始化全局变量失败:"+CommonValueHandle.text2str(ret) ret = self.initMysqlConnect() if not ret: self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,数据库初始化失败,请检查mysql的设置") globalVars.getLogger().info("mysql 数据库初始化失败,请检查mysql的设置:"+CommonValueHandle.text2str(ret)) return "mysql 数据库初始化失败,请检查mysql的设置:"+CommonValueHandle.text2str(ret) if(not self.doPreSql()): self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,执行前置sql失败,请检查sql语句") globalVars.getLogger().error("执行前置sql失败,请检查sql语句") return "执行前置sql失败,请检查sql语句" # ret = self.doSuitePreRequirement() if True !=ret: self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,前置条件执行失败,请检查是否正确") globalVars.getLogger().error("前置条件执行失败,请检查是否正确:"+CommonValueHandle.text2str(ret)) return "前置条件执行失败,请检查是否正确:"+CommonValueHandle.text2str(ret) ret = self.doSuiteCase() if True !=ret: self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,案例执行失败") globalVars.getLogger().error("案例执行失败,请联系管理员:"+CommonValueHandle.text2str(ret)) return "前置条件执行失败,请联系管理员:"+CommonValueHandle.text2str(ret) if(not self.doPostSql()): self.changeTaskStatus(-1) self.saveTaskLastResult("执行失败,执行后置sql失败,请检查sql语句") globalVars.getLogger().error("执行后置sql失败,请检查sql语句") return "执行后置sql失败,请检查sql语句" self.saveTaskLastResult("执行成功!") self.changeTaskStatus(1) return True
def getSqlSetting(request): req = json.loads(request.body) eId = None if req.has_key("eId"): eId = req["eId"] if not eId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: sql = DatabaseSettingModel.objects.filter(env_id=int(eId)) currentSql = None except Exception as e: globalVars.getLogger().error("保存sql设置失败:" + CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "保存sql设置失败"), content_type="application/json") else: if 0 < len(sql): currentSql = sql[0] return HttpResponse(globalVars.responseJson( "true", "", currentSql.getDict()), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true", ""), content_type="application/json")
def updateEnv(request): req = json.loads(request.body) eId = None name = None if req.has_key("eId"): eId = req["eId"] if req.has_key("name"): name = req["name"] if not eId or not name: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: env = EnvModel.objects.get(pk=int(eId)) env.name = name env.save() except Exception as e: globalVars.getLogger().error("修改执行环境失败:" + CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "修改执行环境失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true", "", env.getDict()), content_type="application/json")
def addSystemAssert(request): req = simplejson.loads(request.body) name = None key = None value = "" atype = 0 pId = None if req.has_key("name"): name=req["name"] if req.has_key("key"): key=req["key"] if req.has_key("value"): value=req["value"] if req.has_key("type"): atype=req["type"] if req.has_key("pId"): pId=req["pId"] if not name or not key or not pId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: publicAssert = PublicAssertModel.objects.create(name=name,key=key,value=value,type=atype,project_id=int(pId)) except Exception as e: globalVars.getLogger().error("添加全局断言失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "添加全局断言失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true","",publicAssert.getDict()), content_type="application/json")
def updateTestSuiteCases(request): req = json.loads(request.body) suId = None casesId = None if req.has_key("suId"): suId = req["suId"] if req.has_key("casesId"): casesId = req["casesId"] if not suId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: suite = TestSuiteModel.objects.get(pk=int(suId)) suite.casesId = casesId suite.save() except Exception as e: globalVars.getLogger().error("编辑测试套件失败:" + CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "编辑测试套件失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true", "", suite.getDict()), content_type="application/json")
def getTestSuiteList(request): req = simplejson.loads(request.body) try: pId = req["pId"] except KeyError: pId = None if not pId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: suiteList = TestSuiteModel.objects.filter(project_id=int(pId)) except Exception as e: globalVars.getLogger().error("删除测试套件失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "删除测试套件失败"), content_type="application/json") else: dataList = [] for suite in suiteList: content={} content["name"]=suite.name content["suId"] = suite.id content["dec"] = suite.dec dataList.append(content) return HttpResponse(globalVars.responseJson("true","",dataList), content_type="application/json")
def getTask(self): try: self.task = TaskModel.objects.get(pk=int(self.tId)) except Exception as e: return CommonValueHandle.text2str(e.message) else: return True
def createHistory(self): try: history = TaskHistoryModel() history.taskId = self.id history.taskName = self.name history.suiteId = self.suite.id history.envId = self.env.id history.project = self.project history.taskType = self.taskType #0代表非定时任务,1代表是定时任务 history.repeatDateTime = self.repeatDateTime history.repeatType = self.repeatType #-1代表不重复,1代表每天,3代表每三天,7代表每周 history.status = self.status #0代表未运行,1代表运行中 history.successRate = self.successRate #成功率 history.nextResultVersion = self.nextResultVersion #代表下一次执行的结果的一个标识,就是一个版本号 history.lastResultVersion = self.lastResultVersion #代表上一次执行的结果的一个标识,就是一个版本号 history.lastRunningTime = self.lastRunningTime #上次执行时间 history.lastRunningUser = self.lastRunningUser #上次执行人 history.lastRunningSuccessCount = self.lastRunningSuccessCount #上次成功的的数目 history.lastRunningfailedCount = self.lastRunningfailedCount #上次失败的的数目 history.lastRunningResultIdList = self.lastRunningResultIdList #结果的id列表,用来跟case的id对应起来,本身的排序是跟测试套件里面的case 的排序一致 history.lastRunningPreResultIdList = self.lastRunningPreResultIdList #结果的id列表,用来跟precase的id对应起来,本身的排序是跟测试套件里面的case 的排序一致 history.lastRunningResult = self.lastRunningResult history.save() except Exception as e: globalVars.getLogger().error("历史数据保存失败:" + CommonValueHandle.text2str(e.message)) pass
def updateCommonAssertList(self, List, Commonfield): if not getattr(self, Commonfield) and getattr(self, Commonfield) != "": return -1 com = getattr(self, Commonfield) oldlist = [] try: oldlist = json.loads(com) except Exception as e: globalVars.getLogger().error("json转换失败" + CommonValueHandle.text2str(e.message)) oldlist = [] try: for i in oldlist: a = CustomizeAssertModel.objects.get(pk=int(i)) a.delete() except Exception as e: globalVars.getLogger().error(e.message) re = [] for i in List: assertobj = CustomizeAssertModel() assertobj.updateFromDict(i) re.append(assertobj.id) try: setattr(self, Commonfield, json.dumps(re)) self.save() return 0 except Exception: return -1
def addGlobalValues(request): req = simplejson.loads(request.body) name = None value = "" atype = None pId = None eId = None if req.has_key("name"): name=req["name"] if req.has_key("value"): value=req["value"] if req.has_key("type"): atype=req["type"] if req.has_key("pId"): pId=req["pId"] if req.has_key("eId"): eId=req["eId"] if not name or not value or not pId or not atype: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: values = GlobalValuesModel.objects.create(name=name,value=value,type=atype,project_id=int(pId),env_id=int(eId)) except Exception as e: globalVars.getLogger().error("添加全局断言失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "添加全局断言失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true","",values.getDict()), content_type="application/json")
def getSummary(request): req = json.loads(request.body) pId = None print("~~~~~~~~") print(req) print("6666666") if req.has_key("pId"): pId = req["pId"] if not pId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: taskCount = TaskModel.objects.filter(project_id=int(pId)).count() apiCount = ApiDefine.objects.filter(project_id=int(pId)).count() caseCount = TestCaseModel.objects.filter(project_id=int(pId)).count() suiteCount = TestSuiteModel.objects.filter(project_id=int(pId)).count() except Exception as e: globalVars.getLogger().error("获取结果失败:" + CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "获取结果失败"), content_type="application/json") else: data = {} data["task"] = taskCount data["api"] = apiCount data["case"] = caseCount data["suite"] = suiteCount return HttpResponse(globalVars.responseJson("true", "", data), content_type="application/json")
def getChart(request): req = json.loads(request.body) pId = None print("~~~~~~~~") print(req) print("6666666") if req.has_key("pId"): pId = req["pId"] if not pId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: now = datetime.datetime.now() delta = datetime.timedelta(days=-30) n_days = now + delta tasks = TaskHistoryModel.objects.filter(project_id=int(pId), lastRunningTime__gt=n_days) data = [] for i in tasks: data.append(i.getChartData()) except Exception as e: globalVars.getLogger().error("获取结果失败:" + CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "获取结果失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true", "", data), content_type="application/json")
def updateSuiteRequirement(request): req = simplejson.loads(request.body) suId = None rIds = None if req.has_key("suId"): suId=req["suId"] if req.has_key("rIds"): rIds = req["rIds"] if not suId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: if not rIds: rIds = [] suite = TestSuiteModel.objects.get(pk=int(suId)) suite.updateRequirement(rIds) except Exception as e: globalVars.getLogger().error("修改测试套件的前置条件失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "修改测试套件的前置条件失败"), content_type="application/json") else: content={} content["preRequirement"] = suite.getPreRequirement() content["suId"] = suite.id return HttpResponse(globalVars.responseJson("true","",content), content_type="application/json")
def addTestSuite(request): req = simplejson.loads(request.body) pId = None uId = None name = None dec = None if req.has_key("name"): name=req["name"] if req.has_key("pId"): pId=req["pId"] if req.has_key("dec"): dec = req["dec"] if req.has_key("uId"): uId = req["uId"] if not name or not pId or not dec or not uId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: pro = Project.objects.get(pk=int(pId)) u = Users.objects.get(pk=int(uId)) except Project.DoesNotExist or Users.DoesNotExist: globalVars.getLogger().error("pId,uId 不存在") return HttpResponse(globalVars.responseJson("false", "项目或者用户不存在"), content_type="application/json") else: try: suite = TestSuiteModel.objects.create(name=name,project=pro,dec=dec,user=u) except Exception as e: globalVars.getLogger().error("新建测试套件失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "新建测试套件失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true","",suite.getDict()), content_type="application/json")
def updateTestSuiteBaseInfo(request): req = simplejson.loads(request.body) suId = None name = None dec = None if req.has_key("suId"): suId = req["suId"] if req.has_key("name"): name = req["name"] if req.has_key("dec"): dec = req["dec"] if not suId or not name or not dec: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: suite = TestSuiteModel.objects.get(pk=int(suId)) suite.name=name suite.dec=dec suite.save() except Exception as e: globalVars.getLogger().error("更新测试套件失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "更新测试套件失败"), content_type="application/json") else: content={} content["name"]=suite.name content["suId"] = suite.id content["dec"] = suite.dec return HttpResponse(globalVars.responseJson("true","",content), content_type="application/json")
def runJobByloop(self): if self.__isTimeRight(): globalVars.getLogger().info("开始执行定时任务:") globalVars.getLogger().info(self.name) try: excute = TestTaskExcute(self.taskId,-1) ret = excute.excute() if True!=ret: globalVars.getLogger().error("执行任务失败:"+ret) except Exception as e: globalVars.getLogger().error("定时任务执行失败:"+CommonValueHandle.text2str(e.message)) else: historys = TaskHistoryModel.objects.filter(taskId=int(self.taskId)).order_by("-lastResultVersion") if len(historys) > 0: current = historys[0] name = current.taskName successRate = current.successRate lastRunningSuccessCount = current.lastRunningSuccessCount lastRunningfailedCount = current.lastRunningfailedCount lastRunningTime = current.lastRunningTime message = u"任务 " + name + u" 的执行结果:\n" + u"执行时间: " + str(lastRunningTime) + u", 成功率:" + str( successRate) + u", 成功:" + str(lastRunningSuccessCount) + u", 失败:" + str(lastRunningfailedCount) + \ u"\n 详细报告地址:http://t27.klook.io/history/" + str(current.id) send_mail(u'接口自动化测试报告', message, settings.EMAIL_HOST_USER,settings.EMAIL_RECEIVERS) globalVars.getLogger().info("定时任务执行成功") else: return ""
def addEnv(request): req = simplejson.loads(request.body) pId = None name = None if req.has_key("name"): name=req["name"] if req.has_key("pId"): pId=req["pId"] if not name or not pId: globalVars.getLogger().error("参数不正确,请检查请求参数") return HttpResponse(globalVars.responseJson("false", "参数不正确,请检查请求参数"), content_type="application/json") try: pro = Project.objects.get(pk=int(pId)) except Project.DoesNotExist: globalVars.getLogger().error("pId不存在") return HttpResponse(globalVars.responseJson("false", "项目不存在"), content_type="application/json") else: try: env = EnvModel.objects.create(name=name,project=pro) except Exception as e: globalVars.getLogger().error("新建执行环境失败:"+CommonValueHandle.text2str(e.message)) return HttpResponse(globalVars.responseJson("false", "新建执行环境失败"), content_type="application/json") else: return HttpResponse(globalVars.responseJson("true","",env.getDict()), content_type="application/json")
def run1(q, db_host, db_user, db_psw, db_port, db, sshHost, sshPort, sshUser, sshPsw, sshKey): if not db_host or not db_user or not db_psw or not db_port or not db or not sshHost or not sshPort or not sshUser: globalVars.getLogger().error("参数错误") q.put(0) return if (not sshPsw and not sshKey): globalVars.getLogger().error("参数错误") q.put(0) return try: if not sshPsw: accessory_dir = os.path.join(settings.STATIC_ROOT, "sshKeys") keyUrl = "%s/%s" % (accessory_dir, sshKey) if not os.path.exists(keyUrl): globalVars.getLogger().error(keyUrl + "路径不存在") q.put(0) return with SSHTunnelForwarder( (sshHost, sshPort), ssh_pkey=keyUrl, ssh_username=sshUser, remote_bind_address=(db_host, db_port)) as server: # print server conn = MySQLdb.connect( host='127.0.0.1', port=server.local_bind_port, user=db_user, passwd=db_psw, db=db, charset="utf8", cursorclass=MySQLdb.cursors.DictCursor) else: with SSHTunnelForwarder( (sshHost, sshPort), ssh_password=sshPsw, ssh_username=sshUser, remote_bind_address=(db_host, db_port)) as server: conn = MySQLdb.connect( host='127.0.0.1', port=server.local_bind_port, user=db_user, passwd=db_psw, db=db, charset="utf8", cursorclass=MySQLdb.cursors.DictCursor) except Exception as e: globalVars.getLogger().error( "连接数据库失败:" + CommonValueHandle.text2str(e.message)) traceback() q.put(0) return else: globalVars.getLogger().error("连接数据库失败:") q.put(1) return
def doSuitePreRequirement(self): try: globalVars.getLogger().info("执行前置条件") self.preRequirement = self.suite.getPreRequirement() for i in self.preRequirement: te = TestCaseExcute(i["cId"],self.globalVars,self.runningVars,i["pickerValue"],self.task.env,-1,self.task.nextResultVersion) re = te.excute() if isinstance(re,str): globalVars.getLogger().info("执行前置条件出现错误!"+CommonValueHandle.text2str(re)) return "执行前置条件出现错误!"+CommonValueHandle.text2str(re) reId = te.saveResult(self.task.id,"执行成功") self.preResultIds.append(reId) except Exception as e: globalVars.getLogger().err("前置案例执行失败:"+CommonValueHandle.text2str(e.message)) return CommonValueHandle.text2str(e.message) else: return True
def setCommonData(self, Commonfield, dataList): if not getattr(self, Commonfield) and getattr(self, Commonfield) != "": return Commonfield + ":没有这样的字段" com = getattr(self, Commonfield) jsonData = [] oldJsonData = [] if com == "" or com == None: oldJsonData = [] else: oldJsonData = json.loads(getattr(self, Commonfield)) try: if isinstance(oldJsonData, list): for i in oldJsonData: CommonParmasModel.objects.get(pk=i).delete() res = [] for data in dataList: newData = CommonParmasModel() # newData.api=self if data.has_key("dec"): newData.dec = data["dec"] if data.has_key("default"): newData.default = data["default"] if data.has_key("type"): newData.type = data["type"] if data.has_key("value"): newData.value = data["value"] if data.has_key("name"): newData.name = data["name"] else: globalVars.getLogger().error("header的名称不能为空") return "header的名称不能为空" # print("newData") # print(newData) newData.save() #newData.pId=self.project.id res.append(newData) jsonData.append(newData.id) setattr(self, Commonfield, json.dumps(jsonData)) self.save() except Exception as e: globalVars.getLogger().error(Commonfield + "创建失败" + CommonValueHandle.text2str(e.message)) return Commonfield + "创建失败" + CommonValueHandle.text2str(e.message) else: return res
def updatePostRequirement(self, rIds): try: re = json.dumps(rIds) self.postRequirement = re self.save() except Exception as e: globalVars.getLogger().error("json转换失败" + CommonValueHandle.text2str(e.message)) pass
def getTestCase(self): try: self.testCase = TestCaseModel.objects.get(pk=int(self.cId)) except Exception as e: globalVars.getLogger().error("获取案例失败:" + CommonValueHandle.text2str(e.message)) return False else: return True
def addCommonData(self, Commonfield, data): if not getattr(self, Commonfield) and getattr(self, Commonfield) != "": return Commonfield + ":没有这样的字段" com = getattr(self, Commonfield) jsonData = [] if com == "" or com == None: jsonData = [] else: jsonData = json.loads(getattr(self, Commonfield)) if isinstance(jsonData, list): try: newData = CommonParmasModel() # newData.api=self if data.has_key("dec"): newData.dec = data["dec"] if data.has_key("default"): newData.default = data["default"] if data.has_key("type"): newData.type = data["type"] if data.has_key("value"): newData.value = data["value"] if data.has_key("name"): newData.name = data["name"] else: globalVars.getLogger().error("创建失败,header的名称不能为空") return "header的名称不能为空" # print("newData") #print(newData) newData.save() jsonData.append(newData.id) setattr(self, Commonfield, json.dumps(jsonData)) self.save() except Exception as e: globalVars.getLogger().error( Commonfield + "创建失败" + CommonValueHandle.text2str(e.message)) return Commonfield + "创建失败" + CommonValueHandle.text2str( e.message) else: return newData else: globalVars.getLogger().error(Commonfield + "的数据格式不正确") return Commonfield + "的数据格式不正确"
def dealPostSql(self): try: postSql = self.testCase.postSql globalVars.getLogger().info("执行后置sql:" + str(postSql)) re = self.__excuteSql(postSql) except Exception as e: globalVars.getLogger().info("执行后置sql失败:" + str(postSql) + CommonValueHandle.text2str(e.message)) return False else: return re
def getGlobalValue(self,env): re = {} if not env: return "env环境变量不能为空" try: values = GlobalValuesModel.objects.filter(env=env) except Exception as e: globalVars.getLogger().error("提取全局变量失败:"+CommonValueHandle.text2str(e.message)) return "提取全局变量失败:"+CommonValueHandle.text2str(e.message) else: myfunc = myFuncHandleClass() for i in values: name = i.name if not name: continue value = myfunc.functionReplace(i.value)#覆盖函数 value = self.__getValue(value, i.type) re[name] = value return re
def excute(self): if not self.getTestCase() or not self.getApi(): self.error = True globalVars.getLogger().error("案例获取失败,无法执行,请检查设置") return "案例获取失败,无法执行,请检查设置" if not self.dealPreSql(): self.error = True globalVars.getLogger().error("案例前置条件中的sql语句执行错误,请检查设置.") return "案例前置条件中的sql语句执行错误,请检查设置." self.getHead() globalVars.getLogger().info("head:") globalVars.getLogger().info(self.requestHead) self.getParmas() globalVars.getLogger().info("body:") globalVars.getLogger().info(self.requestBody) re = self.sendReuest() if True != re: self.error = True globalVars.getLogger().error("案例执行错误:" + CommonValueHandle.text2str(re)) return re # response = self.responseBody globalVars.getLogger().info("response:") globalVars.getLogger().info(response) self.getPicker(response) globalVars.getLogger().info("变量提取:") globalVars.getLogger().info(self.runningVars) self.dealSystemAssert(response) globalVars.getLogger().info("断言:") globalVars.getLogger().info(self.systemAssertResult) self.dealDefineAssert(response) globalVars.getLogger().info(self.defineAssertResult) self.dealSqlAssert() globalVars.getLogger().info(self.sqlAssertResult) # re = self.dealPostSql() if not self.dealPostSql(): self.error = True globalVars.getLogger().error("案例后置条件中的sql语句执行错误,请检查设置.") return "案例后置条件中的sql语句执行错误,请检查设置." return True
def dealPreSql(self): try: preSql = self.testCase.preSql globalVars.getLogger().info("执行前置sql:" + str(preSql)) re = self.__excuteSql(preSql) except Exception as e: print(e) traceback.print_exc() globalVars.getLogger().info("执行前置sql失败:" + str(preSql) + CommonValueHandle.text2str(e.message)) return False else: return re