def request(self, method, url, data=None, json=None): method = method.upper() #转换成大写 if type(data) == str: data = eval(data) #拼接请求的url,再传到url=url的后面的url中 url = config.get('api', 'pre_url') + url # print("请求url",url) # print("请求data",data) logger.debug("请求url:{0}".format(url)) logger.debug("请求data:{0}".format(data)) if method == 'GET': resp = self.session.request(method=method, url=url, params=data) elif method == 'POST': if json: resp = self.session.request(method=method, url=url, json=json) else: resp = self.session.request(method=method, url=url, data=data) else: resp = None # print('UN-support method') logger.error('UN-support method') # print("请求response:",resp.text) logger.debug("请求response:{0}".format(resp.text)) return resp
def test_login(self,case): logger.info('开始测试{0}'.format(case.title)) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected,resp.text) self.excel.write_result(case.case_id + 1,resp.text,'PASS') except AssertionError as e: self.excel.write_result(case.case_id + 1,resp.text,'FAIL') logger.error("报错了,{0}".format(e)) raise e
def test_login(self, case): logger.info('开始测试:{}'.format(case.title)) resp = self.http_request.request(case.method, case.url, data=case.data) actual = resp.json()["message"] try: self.assertEqual(case.expected, actual) self.excel.write_result(case.case_id + 1, resp.text, "PASS") except AssertionError as e: self.excel.write_result(case.case_id + 1, resp.text, "FAIL") logger.error('测试报错了:{}'.format(e)) raise e logger.info('结束测试:{}'.format(case.title))
def test_login(self, case): # print(case.title) logger.info('开始测试:{0}'.format(case.title)) resp = self.http_request.request(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) self.excel.write_result(case.case_id + 1, resp.text, "pass") except AssertionError as e: self.excel.write_result(case.case_id + 1, resp.text, "fail") logger.error('测试报错了,{0}'.format(e)) raise e logger.info('结束测试:{0}'.format(case.title))
def request(self, method, url, params=None, data=None, json=None, cookies=None, headers=None): if type(data) == str: data = eval(data) # 拼接请求的url url = config.get('api', 'pre_url') + url logger.debug('请求url:{}'.format(url)) logger.debug('请求的data:{}'.format(data)) if method.lower() == 'get': resp = requests.get(url, params=params, cookies=cookies, headers=headers) # resp是Response对象 elif method.lower() == 'post': if json: # 如果json传参不为空 resp = requests.post(url, json=json, cookies=cookies, headers=headers) else: resp = requests.post(url, data=data, cookies=cookies, headers=headers) else: logger.error('UN-support method') logger.debug('请求response:{}'.format(resp.text)) return resp
def request(self, method, url, data=None, json=None): method = method.upper() if type(data) == str: data = eval(data) # str 转成字典 # 拼接请求的url url = config.get('api', 'pre_url') + url logger.debug('请求url是:{0}'.format(url)) logger.debug('请求data:{0}'.format(data)) if method == 'GET': resp = self.session.request(method=method, url=url, params=data) elif method == 'POST': if json: resp = self.session.request(method=method, url=url, json=json) else: resp = self.session.request(method=method, url=url, data=data) else: resp = None logger.error('UN-support method') logger.debug('请求结果是:{0}'.format(resp.text)) return resp