def test_api(self, item): #请求之前完成loan_id的替换 if item['data'].find('${loan_id}') != -1: if getattr(GetData, 'load_id') == None: query_sql = 'select Id from loan where Member ID={0}'.format( getattr(GetData, 'loan_member_id')) loan_id = DoMysql().do_mysql(query_sql)[0][0] item['data'] = item['data'].replace('${loan_id}', str(loan_id)) setattr(GetData, 'load_id', loan_id) #利用反射存储结果 else: item['data'] = item['data'].replace( '${loan_id}', str(getattr(GetData, 'loan_id'))) res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'Cookie')) if res.cookies: setattr(GetData, 'Cookie', res.cookies) try: self.assertEqual(str(item['expected']), res.json()['code']) TestResult = 'PASS' except AssertionError as e: TestResult = 'Failed' my_logger.info('执行用列出错:{0}'.format(e)) raise e finally: DoExcel.write_back(test_case_path, item['sheet_name'], item['case_id'] + 1, str(res.json()), TestResult) my_logger.error("获取到的结果是:{0}".format(res.json()))
def test_api(self, item): print(test_data) # print("url是:{0},data是:{1},header是:{2},http_method是:{3}".format(item['url'],eval(item['data']),item["header"],item['http_method'])) print("url value:{0}".format(type(item['url']))) print("data value:{0}:".format(type(item['data']))) print("header value:{0}:".format(type(eval(item['header'])))) # print("params value:{0}:".format(type(eval(item['params'])))) print("http_method value:{0}:".format(type(item['http_method']))) res = HttpRequest.http_request(item['url'], item['data'], eval(item["header"]), item['http_method'], getattr(GetData, "Cookie")) if res.cookies: setattr(GetData, 'Cookie', res.cookies) try: self.assertEqual(item['excepted_code'], res.json()["code"]) TestResult = 'PASS' #成功的 except AssertionError as e: TestResult = 'Failed' #失败的 my_logger.info("执行用例出错:{}".format(e)) raise e finally: DoExcel.write_back(test_case_path, 'login', item['case_id'] + 1, str(res.json()), TestResult) print('获取到的结果是:{}'.format(res.json()))
def test_api(self, item): print(item) print(type(item)) print(item["model"]) print(type(item["model"])) if item["model"] == "login_notice": global accessToken res_login = HttpRequest.http_request(item['url'], item['data'], eval(item["header"]), eval(item["params"]), item['http_method'], getattr(GetData, "Cookie")) accessToken = res_login.json()["accessToken"] print("accessToken 值为".format(accessToken)) if res_login.cookies: setattr(GetData, 'Cookie', res_login.cookies) try: self.assertEqual(item['excepted_code'], res_login.json()["code"]) TestResult = 'PASS' # 成功的 except AssertionError as e: TestResult = 'Failed' # 失败的 my_logger.info("执行用例出错:{}".format(e)) raise e finally: DoExcel.write_back(test_case_path, 'notice', item['case_id'] + 1, str(res_login.json()), TestResult) print('获取到的结果是:{}'.format(res_login.json())) elif item["model"] == "notice": sid = accessToken res_notice = HttpRequest.http_request( item['url'], item['data'], eval(item["header"].replace( "5EFCC60C2373B9C1FC526F64CAF24610EFA2BFC1C056AF9C92AFC48CE14EE8D8B13942D7F20742E379D7A6F89DB87D72", sid)), eval(item["params"]), item['http_method'], getattr(GetData, "Cookie")) if res_notice.cookies: setattr(GetData, 'Cookie', res_notice.cookies) try: self.assertEqual(item['excepted_code'], eval(res_notice.json()["retHead"])) TestResult = 'PASS' # 成功的 except AssertionError as e: TestResult = 'Failed' # 失败的 my_logger.info("执行用例出错:{}".format(e)) raise e finally: # DoExcel.write_back(test_case_path,'notice',item['case_id']+1,str(res_notice.json()),TestResult) print('获取到的结果是:{}'.format(res_notice.json()))
def test_api(self,item): res=HttpRequest.http_request(item['method'],item['url'],eval(item['data']),getattr(GetCookie,'Cookie')) if res.cookies: #利用反射存储cookie值 setattr(GetCookie,'Cookie',res.cookies) try: self.assertEqual(str(item['expected']),res.json()['resultcode']) TestResult='PASS' except AssertionError as e: TestResult='Failed' MyLog.error("执行用例出错:{0}".format(e)) raise e finally: DoExcel.write_back(test_case_path,item['sheet_name'],item['case_id']+1,res.text,TestResult) MyLog.info("获取到的结果是:{0}".format(res.text))
def test_api(self, item): res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetCookie, "Cookie")) try: self.assertEqual(item['excepted'], res.json()["status"]) TestResult = 'PASS' #成功的 except AssertionError as e: TestResult = 'Failed' #失败的 print("执行用例出错:{}".format(e)) raise e finally: DoExcel.write_back(test_case_path, 'login', item['case_id'] + 1, str(res.json()), TestResult) print('获取到的结果是:{}'.format(res.json()))
def test_api(self, item): res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetCookie, 'Cookie')) try: self.assertEqual(item['expected'], res.json()['stat']) TestResult = 'PASS' except Exception as e: TestResult = 'Failed' print("执行用例出错:{0}".format(e)) raise e finally: DoExcel.write_back(test_case_path, 'login', item['case_id'] + 1, str(res.json()), TestResult) print("获取到结果是:{0}".format(res.json()))
def test_api(self, item): res = HttpRequest.http_request(item['url'], json.dumps(eval(item['data'])), item['http_method'], eval(item['header'])) try: self.assertEqual('0', res.json()['success']) TestResult = 'PASS' except AssertionError as e: TestResult = 'FAILED' my_logger.info('执行出错:{0}'.format(e)) raise e finally: DoExcel.write_back(tool_path.test_data_path, item['sheet_name'], int(item['case_id']) + 1, str(res.json()), TestResult) print('获取的结果是:{0}'.format(res.json()['msg']))
def test_api(self, item): res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'Cookie')) if res.cookies: #利用反射存取cookies setattr(GetData, 'Cookie', res.cookies) try: self.assertEqual(item['expected'], res.json()['stat']) TestResult = 'PASS' except Exception as e: TestResult = 'Failed' my_logger.info("执行用例出错:{0}".format(e)) raise e finally: DoExcel.write_back(test_case_path, item['sheet_name'], item['case_id'] + 1, str(res.json()), TestResult) my_logger.error("获取到结果是:{0}".format(res.json()))
def test_api(self, item): my_logger.info('开始执行用例{0}:{1}'.format(item['case_id'], item['title'])) loan_member_id = getattr(GetData, 'loan_member_id') # 请求之前完成load_id的替换 if item['data'].find('${loan_id}') != -1: if getattr(GetData, 'loan_id') is None: query_sql = 'select max(id) from loan where member_id={0}'.format(loan_member_id) loan_id = DoMysql().do_mysql(query_sql)[0][0] item['data'] = item['data'].replace('${loan_id}', str(loan_id)) setattr(GetData, 'loan_id', loan_id) my_logger.info('loan_id为:{0}'.format(loan_id)) else: my_logger.info(getattr(GetData, 'loan_id')) item['data'] = item['data'].replace('${loan_id}', str(getattr(GetData, 'loan_id'))) my_logger.info('获取到的请求数据是{0}'.format(item['data'])) if item['check_sql'] != None: # 当你的check_sql的语句不为空时 就可以进行数据库校验 my_logger.info(item['check_sql']) my_logger.info('此条用例需要做数据库校验:{0}'.format(item['title'])) query_sql = eval(item['check_sql'])['sql'] # 拿到Excel中 字典里面的sql语句 Before_Amount = DoMysql.do_mysql(query_sql, 1)[0] my_logger.info('用例:{0}请求之前的余额是{1}'.format(item['title'], Before_Amount)) my_logger.info('---------------------开始http 接口请求---------------------') res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'Cookie')) my_logger.info('---------------------完成http 接口请求---------------------') After_Amount = DoMysql().do_mysql(query_sql, 1)[0] my_logger.info('用例:{0}请求之后的余额是{1}'.format(item['title'], After_Amount)) if float(abs(Before_Amount-After_Amount+1000)) == float(eval(item['data'])['amount']): my_logger.info('数据库余额校验通过') check_sql_result = '数据库检查通过' else: my_logger.info('数据库余额校验未通过') check_sql_result = '数据库检查未通过' # 写回结果 DoExcel.write_back(project_path.test_case_path, item['sheet_name'], item['case_id'] + 1, 10, check_sql_result) else: my_logger.info('此条用例不需要做数据库校验:{0}'.format(item['title'])) my_logger.info('---------------------开始http 接口请求---------------------') res = HttpRequest.http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'Cookie')) my_logger.info('---------------------完成http 接口请求---------------------') if res.cookies: # 利用反射存储cookie值 setattr(GetData, 'Cookie', res.cookies) try: self.assertEqual(str(item['expected']), res.json()['code']) test_result = 'PASS' # 成功的 except AssertionError as e: test_result = 'Failed' # 失败的 my_logger.error("执行用例出错:{0}".format(e)) raise e finally: # 不管try是错还是对,finally里面的代码是一定会执行的 DoExcel.write_back(project_path.test_case_path, item['sheet_name'], item['case_id'] + 1, 8, str(res.json())) DoExcel.write_back(project_path.test_case_path, item['sheet_name'], item['case_id'] + 1, 9, test_result) my_logger.info("获取到的结果是:{0}".format(res.json())) my_logger.info('----------------------------------------------------------------------------------\n')