def excel_suite(): register_excel = HandleExcel(DATA_COMMON_FILE_PATH, 'add') # 实例化对象 register_excel_cases = register_excel.get_cases() # 获取excel测试用例 register_cases = HandleContext.borrower_user_id( HandleContext.manager_user_pwd( HandleContext.manager_user_tel( str(register_excel_cases)))) # 执行参数化替换 register_cases = eval(register_cases) wb, ws = register_excel.load_excel() wb.close() return register_cases
def excel_suite(): register_excel = HandleExcel(DATA_COMMON_FILE_PATH, 'recharge') # 实例化对象 register_excel_cases = register_excel.get_cases() # 获取excel测试用例 register_cases = HandleContext.not_existed_tel( HandleContext.investors_user_pwd( HandleContext.investors_user_tel( str(register_excel_cases)))) # 执行参数化替换 register_cases = eval(register_cases) wb, ws = register_excel.load_excel() wb.close() return register_cases
def test_user_register(self, one_case): logger.info("正在执行第{0}条用例:{1}".format(one_case["case_id"], one_case["title"])) url_new = config_read_file.get_value("api", "url") + one_case["url"] new_data = HandleContext.reginster_paramization(one_case["data"]) logger.info("\n请求url为{0}\ndata为{1}".format(url_new, new_data)) register = self.resp.sendRequests(method=one_case["method"], url=url_new, data=eval(new_data)) # 返回注册响应对象 try: self.assertEqual(one_case["expected"], register.text, msg=one_case["title"]) result = "True" logger.info("\'{0}\'用例执行成功".format(one_case["title"])) except AssertionError as err: logger.error("{0}用例执行失败,错误信息为: {1}".format(one_case["title"], err)) result = "False" raise err finally: logger.info("写入结果开始") do_excel.write_result(row=one_case["case_id"] + 1, column=8, result=result) do_excel.write_result(row=one_case["case_id"] + 1, column=7, result=register.text) logger.info("写入结果结束")
def test_login(self,one_case): case_id = one_case['case_id'] title = one_case['title'] data = one_case['data'] new_data = HandleContext.login_paramaterization(data) url = one_case['url'] total_url = do_config.get_value('requests','url_head')+url method = one_case['method'] expected = one_case['expected'] header = do_config.get_eval_data('requests','login_headers') login_res = do_http_requests.to_request(method=method, url=total_url, data=new_data, headers=header) real_result = login_res.text expected_result = expected msg = '测试' + title success_msg = do_config.get_value('msg', 'success_result') fail_msg = do_config.get_value('msg', 'fail_result') try: self.assertEqual(expected_result,real_result,msg=msg) handexcel.write_case(case_id+1,real_result,success_msg) do_logger.debug('{},用例执行成功,执行结果为:{}'.format(msg,success_msg)) except AssertionError as e: handexcel.write_case(case_id+1,real_result,fail_msg) do_logger.debug('{},用例用例执行失败,结果为:{},失败原因:{}'.format(msg, fail_msg, e)) raise e
def test_register(self,one_case): case_id = one_case['case_id'] title = one_case['title'] data = one_case['data'] # 需要参数化, 将手机号替换 new_data = HandleContext.register_paramaterization_not_exist(data) #参数已经被正常实际好替换 do_logger.debug('获取到的data数据为:{},在第【{}】'.format(new_data,case_id+1)) url = one_case['url'] total_url = do_config.get_value('requests','url_head') + url method = one_case['method'] expected = one_case['expected'] header = do_config.get_eval_data('requests','login_headers') register_res =do_http_requests.to_request(method=method, url=total_url, data=new_data, headers=header) #real_result = login_res.json()['msg'] real_result = register_res.text #返回是字符串格式json #real_result = str(register_res.json()) # expected_result = expected msg = '测试' + title success_msg = do_config.get_value('msg', 'success_result') fail_msg = do_config.get_value('msg', 'fail_result') try: self.assertEqual(expected_result,real_result,msg=msg) do_hanle_excel.write_case(case_id+1,real_result,success_msg) do_logger.debug('{},用例执行成功,执行结果为:{}'.format(msg,success_msg)) except AssertionError as e: do_hanle_excel.write_case(case_id+1,real_result,fail_msg) do_logger.debug('{},用例用例执行失败,结果为:{},失败原因:{}'.format(msg,fail_msg,e)) raise e
def test_register(self, data_namedtuple): """ 测试注册功能 :return: """ url = do_config("api", "url") + data_namedtuple.url new_data = HandleContext().register_parameterization(data_namedtuple.data) do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) response = self.send_request(method=data_namedtuple.method, url=url, data=new_data) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,http请求状态码为【{}】". format(data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) raise e try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
def test_invest(self, data_namedtuple): """ 测试投资功能 :return: """ do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) method = data_namedtuple.method url = do_config("api", "url") + data_namedtuple.url print(url) new_data = HandleContext().invest_parameterization(data_namedtuple.data) print(new_data) response = self.send_request(method=method, url=url, data=new_data) print(response.text) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,http请求状态码为【{}】". format(data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) raise e # 判断是否加标成功,加标成功,获取loan_id if response.json().get("msg") == "加标成功": check_sql = data_namedtuple.check_sql if check_sql: check_sql = HandleContext().invest_parameterization(check_sql) mysql_data = self.handle_mysql(sql=check_sql) # 获取一条记录 HandleContext.loan_id = mysql_data["Id"] # 动态添加属性,获取标的Id # setattr(HandleContext, "loan_id", mysql_data["Id"]) # 动态创建类属性,获取标的Id # 验证预期结果是否与实际结果相同 try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
def test_invest(self,one_case): case_id = one_case['case_id'] title = one_case['title'] data = one_case['data'] # 需要参数化, 将手机号替换 #换位recharge的参数化 new_data = HandleContext.invest_paramaterization(data) #参数已经被正常实际好替换 do_logger.debug('获取到的new_data数据为:{},在第【{}】'.format(new_data,case_id+1)) url = one_case['url'] total_url = do_config.get_value('requests','url_head') + url method = one_case['method'] expected = one_case['expected'] check_sql = one_case['check_sql'] header = do_config.get_eval_data('requests','login_headers') invest_res =do_http_requests.to_request(method=method, url=total_url, data=new_data, headers=header) invest_res_content = invest_res.text if '加标成功' in invest_res_content: # 确定一定是加标成功 if check_sql: check_sql = HandleContext.invest_paramaterization(check_sql) mysql_data = self.handle_mysql.run_mysql(check_sql) loan_id = mysql_data['Id'] do_logger.debug('load_id的值为【{}】,在第 {} 行,返回值类型 {}'.format(loan_id,case_id+1,type(loan_id))) # 使用全局变量来解决接口依赖,极有可能出现循环导入,会抛出异常 #接口依赖是动态获取变量的过程 #HandleContext.loan_id = loan_id setattr(HandleContext,"loan_id",loan_id) # 给一个对象动态的创建属性 real_result = invest_res.json().get('code') #返回是字符串格式json expected_result = expected msg = '测试' + title success_msg = do_config.get_value('msg', 'success_result') fail_msg = do_config.get_value('msg', 'fail_result') try: self.assertEqual(str(expected_result),real_result,msg=msg) do_hanle_excel.write_case(case_id+1,real_result,success_msg) do_logger.debug('{},用例执行成功,执行结果为:{}'.format(msg,success_msg)) except AssertionError as e: do_hanle_excel.write_case(case_id+1,real_result,fail_msg) do_logger.debug('{},用例用例执行失败,结果为:{},失败原因:{}'.format(msg,fail_msg,e)) raise e
def test_user_login(self, one_case): logger.info("正在执行第{0}条用例:{1}".format(one_case["case_id"], one_case["title"])) url_new = config_read_file.get_value("api", "url") + one_case["url"] new_data = HandleContext.invest_paramization(one_case["data"]) logger.info("\n请求url为{0}\ndata为{1}".format(url_new, new_data)) invest = self.resp.sendRequests(method=one_case["method"], url=url_new, data=eval(new_data)) # 返回投资的响应对象 if "加标成功" in invest.text: check_sql = one_case["check_sql"] if check_sql: check_sql = HandleContext.invest_paramization(check_sql) mysql_result = self.sql.run_sql(check_sql) setattr(HandleContext, "loan_id", mysql_result.get("Id")) logger.info("\n响应数据为{1}".format(url_new, invest.text)) try: self.assertEqual(one_case["expected"], invest.text, msg=one_case["title"]) logger.info("\'{0}\'用例执行成功".format(one_case["title"])) result = "True" except AssertionError as err: logger.error("{0}用例执行失败,错误信息为: {1}".format(one_case["title"], err)) result = "False" raise err finally: logger.info("写入结果开始") do_excel.write_result(row=one_case["case_id"] + 1, column=8, result=result) do_excel.write_result(row=one_case["case_id"] + 1, column=7, result=invest.text) logger.info("写入结果结束")
def test_invest(self, case_list): case_list_str = str(case_list) context_data = HandleContext.manager_user_tel(case_list_str) context_data = HandleContext.manager_user_pwd(context_data) context_data = HandleContext.borrower_user_id(context_data) context_data = HandleContext.loan_id(context_data) context_data = HandleContext.investors_user_tel(context_data) context_data = HandleContext.investors_user_id(context_data) context_data = HandleContext.investors_user_pwd(context_data) context_data = HandleContext.not_existed_user_id(context_data) context_data = HandleContext.not_exitsed_loan_id(context_data) case_list_dict = eval(context_data) request_data = case_list_dict['data'] method = case_list_dict['method'] url = do_config.get_value( 'request', 'default_address') + case_list_dict['url_path'] res = do_request.send_request(method, url, data=request_data) add_success_msg = do_config.get_value('request', 'add_success_msg') actual = res.text if add_success_msg in actual: check_sql = case_list_dict['check_sql'] if check_sql: loan_id_sql = do_mysql.sql_search(check_sql) loan_id_value = loan_id_sql['Id'] setattr(HandleContext, 'loan_idw', loan_id_value) result = str(case_list_dict['expected']) msg = case_list_dict['title'] true_result = do_config.get_value('msg', 'true_result') fail_result = do_config.get_value('msg', 'fail_result') try: self.assertIn(result, actual, msg) print('{},执行结果为:{}'.format(msg, true_result)) my_excel.write_result(case_list_dict['case_id'] + 1, actual, true_result) do_logger.error("{}, 执行结果为: {}".format(msg, true_result)) except AssertionError as e: print('具体异常为{}'.format(e)) my_excel.write_result(case_list_dict['case_id'] + 1, actual, fail_result) do_logger.error("{}, 执行结果为: {},具体异常为{}".format( msg, fail_result, e)) raise e
def test_register(self, one_case): case_id = one_case['case_id'] title = one_case['title'] data = one_case['data'] # 需要参数化, 将手机号替换 new_data = HandleContext.add_paramaterization(data) # 参数已经被正常实际好替换 do_logger.debug('new_data为:{},类型【{}】在第【{}】'.format( new_data, type(new_data), case_id + 1)) # if case_id > 1: # new_data_update = json.loads(new_data, encoding='utf-8') # # do_logger.debug( # 'new_data_update为:{},类型【{}】在第【{}】'.format(new_data_update, type(new_data_update), case_id + 1)) # memberId = new_data_update['memberId'] # do_logger.debug('memberID为:{},类型【{}】在第【{}】'.format(memberId, type(memberId), case_id + 1)) url = one_case['url'] total_url = do_config.get_value('requests', 'url_head') + url method = one_case['method'] expected = one_case['expected'] header = do_config.get_eval_data('requests', 'login_headers') register_res = do_http_requests.to_request(method=method, url=total_url, data=new_data, headers=header) # real_result = login_res.json()['msg'] real_result = register_res.json().get('code') # 返回是字符串格式json expected_result = expected msg = '测试' + title success_msg = do_config.get_value('msg', 'success_result') fail_msg = do_config.get_value('msg', 'fail_result') try: self.assertEqual(str(expected_result), real_result, msg=msg) do_hanle_excel.write_case(case_id + 1, real_result, success_msg) do_logger.debug('{},用例执行成功,执行结果为:{}'.format(msg, success_msg)) except AssertionError as e: do_hanle_excel.write_case(case_id + 1, real_result, fail_msg) do_logger.debug('{},用例用例执行失败,结果为:{},失败原因:{}'.format( msg, fail_msg, e)) raise e
def test_add(self, data_namedtuple): """ 测试加标功能 :return: """ url = do_config("api", "url") + data_namedtuple.url new_data = HandleContext().add_parameterization(data_namedtuple.data) print(new_data) do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) # 接口不支持json格式,只支持form表单,is_json=False, 使用data response = self.send_request(method=data_namedtuple.method, url=url, data=new_data) print(response.text) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,请求状态码为【{}】" .format(data_namedtuple.title, response.status_code)) except AssertionError as e : # 出现异常,后面代码不会执行 do_log.error("{},执行结果:{}\n具体异常信息:{}\n".format(data_namedtuple.title, "fail", e)) raise e try: self.assertEqual(data_namedtuple.expected, response.text, msg="测试{}失败".format(data_namedtuple.title)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id+1, actual=response.text, result=do_config("msg", "success_result"))
import os import unittest import json from libs.ddt import ddt, data from scripts.handle_excel import HandleExcel from scripts.content_os import EXCEL_FILE_PATH, CONFIG_FILE_PATH from scripts.handle_config import HandleConfig from scripts.handle_context import HandleContext from scripts.handle_mysql import HandleMySql from scripts.handle_logging import HandleLog from scripts.handle_webservice_request import HandleWebserviceRequest do_log = HandleLog().get_logger() do_context = HandleContext() do_conf = HandleConfig(os.path.join(CONFIG_FILE_PATH, "api_config.ini")) excel_dir = os.path.join(EXCEL_FILE_PATH, "webservice_api_test_cases.xlsx") do_excel = HandleExcel(excel_dir, "register") cases = do_excel.get_all_case() @ddt class TestRegister(unittest.TestCase): """ 测试注册接口封装类 """ @classmethod def setUpClass(cls): do_log.debug("{:=^50s}".format("注册接口测试用例开始执行"))
def test_recharge(self, one_case): case_id = one_case['case_id'] title = one_case['title'] data = one_case['data'] # 需要参数化, 将手机号替换 #换位recharge的参数化 new_data = HandleContext.recharge_paramaterization(data) #参数已经被正常实际好替换 do_logger.debug('获取到的data数据为:{},在第【{}】'.format(new_data, case_id + 1)) url = one_case['url'] total_url = do_config.get_value('requests', 'url_head') + url method = one_case['method'] expected = one_case['expected'] #充值之前查询金额 check_sql = one_case['check_sql'] if check_sql: check_sql = HandleContext.recharge_paramaterization(check_sql) mysql_data = self.handle_mysql.run_mysql(check_sql) amount_before_regarge = float( mysql_data['LeaveAmount']) # 返回decimal类型,需要用float转换 amount_before_regarge = round(amount_before_regarge, 2) header = do_config.get_eval_data('requests', 'login_headers') register_res = do_http_requests.to_request(method=method, url=total_url, data=new_data, headers=header) #real_result = login_res.json()['msg'] real_result = register_res.json().get('code') #返回是字符串格式json expected_result = expected msg = '测试' + title success_msg = do_config.get_value('msg', 'success_result') fail_msg = do_config.get_value('msg', 'fail_result') try: self.assertEqual(str(expected_result), real_result, msg=msg) if check_sql: #check_sql = HandleContext.recharge_paramaterization(check_sql) #充值后的金额 mysql_data = self.handle_mysql.run_mysql(check_sql) amount_after_regarge = float( mysql_data['LeaveAmount']) # 返回decimal类型,需要用float转换 amount_after_regarge = round(amount_after_regarge, 2) amount_expect_charge = json.loads(data, encoding='utf-8')['amount'] do_logger.debug('{}....类型{}, 在第【{}】行'.format( amount_expect_charge, type(amount_expect_charge), case_id + 1)) #将数据中计算出的充值前后的差值,再次转换为 取后2位小数 amount_sql_recharge = round( amount_after_regarge - amount_before_regarge, 2) self.assertEqual(amount_expect_charge, amount_sql_recharge, msg=msg) do_hanle_excel.write_case(case_id + 1, real_result, success_msg) do_logger.debug('{},用例执行成功,执行结果为:{}'.format(msg, success_msg)) except AssertionError as e: do_hanle_excel.write_case(case_id + 1, real_result, fail_msg) do_logger.debug('{},用例用例执行失败,结果为:{},失败原因:{}'.format( msg, fail_msg, e)) raise e
def test_recharge(self, data_namedtuple): """ 测试充值功能 :return: """ do_log.info("\n正在运行的测试用例为:{}".format(data_namedtuple.title)) method = data_namedtuple.method url = do_config("api", "url") + data_namedtuple.url new_data = HandleContext().recharge_parameterization( data_namedtuple.data) check_sql = data_namedtuple.check_sql # 充值之前用户的剩余金额 if check_sql: check_sql = HandleContext().recharge_parameterization(check_sql) mysql_data = self.handle_mysql(sql=check_sql) # 获取一条记录 amount_before_recharge = float( mysql_data["LeaveAmount"]) # decimal类型数据转换成float类型 amount_before_recharge = round(amount_before_recharge, 2) # 保留两位小数 response = self.send_request(method=method, url=url, data=new_data) # 判断服务器是否有异常 try: self.assertEqual(200, response.status_code, msg="测试【{}】失败,http请求状态码为【{}】".format( data_namedtuple.title, response.status_code)) except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) raise e response = response.text # 获取json格式数据 # 将复杂的数据赋值为空,之后单独验证 if check_sql: response = json.loads(response, encoding="utf8") response["data"] = None response = json.dumps( response, ensure_ascii=False) # ensure_ascii为False,可将乱码转换成中文 # 验证预期结果是否与实际结果相同 try: self.assertEqual(data_namedtuple.expected, response, msg="测试{}失败".format(data_namedtuple.title)) # 充值之后用户的剩余金额 if check_sql: check_sql = HandleContext().recharge_parameterization( check_sql) mysql_data = self.handle_mysql(sql=check_sql) amount_after_recharge = float(mysql_data["LeaveAmount"]) actual_amount = round(amount_after_recharge, 2) recharge_amount = float( json.loads(new_data, encoding="utf8").get("amount")) excepted_amount = round( amount_before_recharge + recharge_amount, 2) # 服务器返回复杂数据的验证,这里验证充值金额 self.assertEqual(excepted_amount, actual_amount, msg="数据库中充值的金额有误") except AssertionError as e: do_log.error("\n具体异常信息为:{}\n".format(e)) self.do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response, result=do_config("msg", "fail_result")) raise e else: self.do_excel.write_result(row=data_namedtuple.case_id + 1, actual=response, result=do_config( "msg", "success_result"))