def send_request(self): headers = self.__handle_header() data = self.__handle_date() url = self.__pre_url() if self.method.upper() == 'POST': try: res = requests.post(url=url, data=data, headers=headers) except Exception: e = logger.exception("发起post请求时候报错") raise e return res
def send_requests(self,token=None,cookie=None,**kwargs): # 获取请求头部信息 headers = self.__handle_header(token,**kwargs) # 对excel中url是否带'/'进行兼容处理 url = self.__pre_url () # 对excel中接口所传的字符串类型的字典参数进行json化处理 data=self.__pre_data (token) logger.info ("请求头 -> {}".format (headers)) logger.info ("请求方法 -> {}".format (self.method)) logger.info ("请求体 -> {}".format (data)) # 根据方法进行调用 if self.method.upper() == "POST": try: res = requests.post(url,json=data,headers=headers,cookies=cookie) except Exception: e = logger.exception("调用post封装方法,请求{}地址时出错".format(url)) raise e elif self.method.upper() =='PATCH': try: logger.info("======>>>开始执行patch请求,数据为{} 类型为{}".format(data,type(data))) logger.info("======>>>开始执行patch请求,请求投为为{} 类型为{}".format(headers,type(headers))) res = requests.patch(url,json=data,headers=headers,cookies=cookie) except Exception: e=logger.exception("调用patch封装方法,请求{}地址时出错".format(url)) raise e else: try: res =requests.get(url,params=data,headers=headers,cookies=cookie) except Exception: e = logger.exception ("调用post封装方法,请求{}地址时出错".format (url)) raise e logger.info ("响应状态码 -> {}".format (res.status_code)) logger.info("响应头 -> {}".format(res.headers)) logger.info("请求头为--> {}".format(res.request.headers)) logger.info ("响应体【实际结果】 -> {}".format (res.json())) return res
if case['check_sql'] is not None: if len(eval(case['check_sql'])) > 1: self.assertEqual( expect_result['data']['member_id'], actual_result.json()['data']['member_id']) self.assertEqual( expect_result['data']['loan_id'], actual_result.json()['data']['loan_id']) last_line = db.get_count( eval(case['check_sql'])['sql_02']) self.assertEqual(pre_line + 1, last_line) test_result = 'Passed' except AssertionError as e: logger.exception("第{}条用例执行-->【失败】".format( self.__class__.count)) test_result = 'Failed' raise e finally: if actual_result: self.t.write_back_data(self.__class__.count + 1, 10, str(actual_result.json())) logger.info("第{}条用例执行的实际结果{}回写成功".format( self.__class__.count, str(actual_result.json()))) self.t.write_back_data(self.__class__.count + 1, 11, str(test_result)) logger.info("第{}条用例执行的测试结果{}回写成功".format( self.__class__.count, test_result)) else: logger.info("第{}条用例执行的实际结果与测试结果回写失败".format( self.__class__.count))
if case['extract_data']: extract_data_from_excel(case['extract_data'], actual_result.json()) try: if case['type'] == 'case': self.assertEqual(expect_result['code'], actual_result.json()['code']) self.assertEqual(expect_result['msg'], actual_result.json()['msg']) if case['check_sql']: self.assertFalse(expect_result['data']['id'] >= actual_result.json()['data']['id']) last_line = db.get_count(eval(case['check_sql'])['sql_02']) logger.info( "expect_line----------->{}".format(expect_line)) logger.info("last_line----------->{}".format(last_line)) self.assertFalse(expect_line >= last_line) test_result = "PASS" except AssertionError as e: logger.exception("---------->第{}条测试用例断言错误----------------".format( self.__class__.count)) test_result = "Failed" raise e finally: if actual_result: self.t.write_back_data(self.__class__.count + 1, 10, str(actual_result.json())) self.t.write_back_data(self.__class__.count + 1, 11, str(test_result)) logger.info("---------->第{}条测试用列回写失败---------".format( self.__class__.count))