def getMockTokenData(self): mockSign = phoneCf.get("mock", "sign") logger.info("mockSign=%s,type=%s" % (mockSign, type(mockSign))) if mockSign in ["True", "true"]: logger.info("getMockTokenData--默认token生成开始") return loadJsonFromFile( os.sep.join([os.getcwd(), "config", "mockToken.json"])) else: return None
def wrapper(*args, **kwargs): logger.info(msg="前置调用函数%s,类%s" % (fun.__name__, str(args[0]))) start = time.time() if userType is not None: args[0].inputKV["userType"] = userType rsp = fun(*args, **kwargs) if userType is not None: del args[0].inputKV["userType"] runtime = time.time() - start return rsp
def httpGet(url="", headers={}): logger.info("http request type:GET") logger.info("http request url:%s" % url) logger.info("http request headers:%s" % jsonFmtPrint(jsondata=headers)) httpRsp = requests.get(url=url, headers=headers, verify=False) a = "{'a':'测试'}" logger.info("测试http response data:%s" % jsonFmtPrint(a)) print("李涛军---" + httpRsp.text) logger.info("测试http response data:%s" % jsonFmtPrint(httpRsp.text)) return httpRsp.text
def httpPost(url="", headers={}, reqJsonData={}): logger.info("http request type:POST") logger.info("http request url:%s" % url) logger.info("http request headers:%s" % jsonFmtPrint(jsondata=headers)) logger.info("http request data:%s" % jsonFmtPrint(reqJsonData)) httpRsp = requests.post(url=url, json=reqJsonData, headers=headers, verify=False) logger.info("http response data:%s" % jsonFmtPrint(httpRsp.text)) return httpRsp.text
def getRetcodeByRsp(self, response=None, format="code"): retcode = "000000" try: retcode = query_json(json_content=json.loads(self.rsp), query=format) except Exception as e: print("返回报文异常") print(e) logger.info("返回报文异常") logger.info(e) retcode = "2000001" finally: return retcode
def initTokenData(self): logger.info("默认token生成开始") self.tkdict = self.getMockTokenData() if self.tkdict is not None: logger.info("使用mock token开始") return else: self.tkdict = defaultdict(dict) wx_token = self.weixinLogin( phoneNum=phoneCf.get("weixin", "phoneNums")) # cms_admin_token = self.cmsLogin(phoneNum=phoneCf.get("admin","admin")) cms_operate_token = self.cmsLogin( phoneNum=phoneCf.get("admin", "operate")) cms_merchants_token = self.cmsLogin( phoneNum=phoneCf.get("admin", "merchants")) hx_merchants_token = self.merchantsLogin( phoneNum=phoneCf.get("merchants", "phoneNums")) print( "wx_token=%s ,cms_operate_token=%s , cms_merchants_token=%s ,hx_merchants_token =%s" % (wx_token, cms_operate_token, cms_merchants_token, hx_merchants_token)) self.tkdict["weixin"][phoneCf.get("weixin", "phoneNums")] = wx_token self.tkdict["admin"]["admin"] = {} self.tkdict["admin"]["operate"] = {} self.tkdict["admin"]["merchants"] = {} self.tkdict["admin"]["operate"][phoneCf.get( "admin", "operate")] = cms_operate_token self.tkdict["admin"]["merchants"][phoneCf.get( "admin", "merchants")] = cms_merchants_token self.tkdict["merchants"][phoneCf.get("merchants", "phoneNums")] = hx_merchants_token logger.info(jsonFmtPrint(self.tkdict)) filePath = os.sep.join([os.getcwd(), "config", "mockToken.json"]) writeStrToJsonFile(filePath=filePath, jsonStr=self.tkdict) logger.info("默认token生成结束")
def httpPutGet(url="", headers={}): logger.info("http request type:GET") logger.info("http request url:%s" % url) logger.info("http request headers:%s" % jsonFmtPrint(jsondata=headers)) httpRsp = requests.put(url=url, headers=headers, verify=False) logger.info("http response data:%s" % jsonFmtPrint(httpRsp.text)) return httpRsp.text
def httpPostFile(url="", headers={}, file=None): logger.info("http request type:POST") logger.info("http request url:%s" % url) logger.info("http request headers:%s" % jsonFmtPrint(jsondata=headers)) httpRsp = requests.post(url=url, data={ 'enctype': 'multipart/form-data', 'name': 'wang' }, files=file, headers=headers, verify=False) logger.info("http response data:%s" % jsonFmtPrint(httpRsp.text)) return httpRsp.text
def compareRetcodeTest(self): logger.info(msg="") self.rsp = self.myservice.sendHttpReq() retcode = self.myservice.getRetcodeByRsp(response=self.rsp) self.assertTrue(retcode == self.expectdata["code"], msg="expect code is %s,actual code is %s,rsp is %s" % (self.expectdata["code"], retcode, self.rsp)) compareDataList = self.expectdata.get("compare", []) for compareData in compareDataList: testPoint = compareData["comparePoint"] expectData = compareData["expectData"] cprFun = getattr(self, compareData["fun"]) sign = compareData.get("sign", True) if sign: expectFunData = self.myservice.compareFuncDict.get( expectData, expectData) expectData = expectFunData() if (hasattr( expectFunData, '__call__')) else expectFunData actualData = self.myservice.compareFuncDict.get( compareData["actualData"])() cprFun( expectData, actualData, "testPoint is %s,expectData is %s,actualData is %s" % (testPoint, str(expectData), str(actualData)))
def setUp(self): logger.info(msg="类=%s,接口=%s,用例ID=%s执行开始" % (self.__class__, self.__interfaceName__, self.getCaseid())) predata = self.getPreConditions() if predata is not None: # dbsqlls = [sql for sql in predata if sql.startswith("preDB")] for pre in predata: if pre.startswith("setup"): if pre in self.myservice.ifacedict: preReqJsonFile = self.myservice.ifacedict[pre][0] if preReqJsonFile is not None: inputFormat = self.myservice.inputKV["reqjsonfile"] self.myservice.inputKV[ "reqjsonfile"] = self.myservice.inputKV[ preReqJsonFile] logger.info(msg="前置条件%s开始执行" % pre) self.myservice.ifacedict[pre][1]() logger.info(msg="前置条件%s结束执行" % pre) if preReqJsonFile is not None: self.myservice.inputKV["reqjsonfile"] = inputFormat
def tearDown(self): predata = self.getPreConditions() if predata is not None: # dbsqlls = [ sql for sql in predata if sql.startswith("tearDB") ] # self.myservice.handlingDb(dbsqlls) for pre in predata: if pre.startswith("tearDown"): if pre in self.myservice.ifacedict: preReqJsonFile = self.myservice.ifacedict[pre][0] if preReqJsonFile is not None: inputFormat = self.myservice.inputKV["reqjsonfile"] self.myservice.inputKV[ "reqjsonfile"] = self.myservice.inputKV[ preReqJsonFile] logger.info(msg="后置步骤%s开始执行" % pre) self.myservice.ifacedict[pre][1]() logger.info(msg="后置步骤%s结束执行" % pre) if preReqJsonFile is not None: self.myservice.inputKV["reqjsonfile"] = inputFormat if pre.startswith("tearDB"): self.myservice.handlingOneDb(pre) logger.info(msg="类=%s,接口=%s,用例ID=%s执行结束" % (self.__class__, self.__class__.__interfaceName__, self.getCaseid()))
def wrapper(*args, **kwargs): logger.info(msg="前置调用函数%s,类%s" % (fun.__name__, str(args[0]))) start = time.time() rsp = fun(*args, **kwargs) runtime = time.time() - start return rsp