class GYHandler(): """任务:中间层 common和调用层""" yaml_path = os.path.join(path.config_path, 'config.yaml') yaml_config = read_yaml(yaml_path) user_path = os.path.join(path.config_path, 'security.yaml') user_config = read_yaml(user_path) # logger logger_file = os.path.join(path.logs_path, yaml_config['logger']['file']) logger = get_logger(name=yaml_config['logger']['name'], file=logger_file) # excel excel_file = os.path.join(path.data_path, 'cases.xlsx') excel = ExcelHandler(excel_file) # 数据库 db_class = MidDBHandler @staticmethod def generate_new_phone(): """自动生成手机号码""" fk = Faker(locale="zh_CN") while True: phone = fk.phone_number() db = MidDBHandler() phone_in_db = db.query( 'select * from member where mobile_phone= {}'.format(phone)) db.close() if not phone_in_db: return phone
class MiddleHandler: """中间层处理""" # yaml模块 yaml_data = get_yaml_data(file_name=config_file_path) # 需要替换的数据 admin_user_name = yaml_data["admin_user"]["user_name"] admin_user_pwd = yaml_data["admin_user"]["password"] loan_user_name = yaml_data["loan_user"]["user_name"] loan_user_pwd = yaml_data["loan_user"]["password"] invest_user_name = yaml_data["invest_user"]["user_name"] invest_user_pwd = yaml_data["invest_user"]["password"] phone_number = "" @classmethod def replace_data(cls, string, pattern="#(.*?)#"): """替换数据""" replace_data = re.finditer(pattern=pattern, string=string) for data in replace_data: old_data = data.group() new_data = str(getattr(cls, data.group(1))) string = string.replace(old_data, new_data) return string # logger模块 my_logger = get_logger(logger_name=yaml_data["logger"]["logger_name"], logger_level=yaml_data["logger"]["logger_level"], stream_handler_level=yaml_data["logger"]["stream_handler_level"], file_handler_level=yaml_data["logger"]["file_handler_level"], file_name=logs_file_path, format_data=yaml_data["logger"]["format_data"]) # excel模块 my_excel = ExcelHandler(file_path=data_file_path) # db模块 db_class = MiddleDb # support模块 @classmethod def get_random_phone_num(cls): """获取任意手机号""" fake = faker.Faker(locale="zh_CN") while True: phone_num = fake.phone_number() my_db = MiddleHandler.db_class() phone_num_db = my_db.query_db(sql="select * from member where mobile_phone = {};".format(phone_num)) my_db.close_db() if not phone_num_db: cls.phone_number = phone_num return phone_num
class YZHandler(): """任务:中间层。 common 和 调用层。 使用项目的配置数据,填充common模块 """ # 替换数据 # 新手机号码 new_phone = '' investor_user_id = '' investor_user_token = '' admin_user_id = '' admin_user_token = '' loan_user_id = '' loan_user_token = '' yaml_path = os.path.join(path.config_path, 'config.yaml') yaml_config = read_yaml(yaml_path) user_path = os.path.join(path.config_path, 'security.yaml') user_config = read_yaml(user_path) # logger logger_file = os.path.join(path.logs_path, yaml_config['logger']['file']) logger = get_logger(name=yaml_config['logger']['name'], file=logger_file) # excel对象 excel_file = os.path.join(path.data_path, 'cases.xlsx') excel = ExcelHandler(excel_file) # 辅助函数 # help_funcs = helper # 数据库 db_class = MidDBHandler @classmethod def generate_new_phone(cls): """自动生成手机号""" fk = Faker(locale='zh_CN') while True: phone = fk.phone_number() db = MidDBHandler() phone_in_db = db.query( 'SELECT * FROM member WHERE mobile_phone = {}'.format(phone)) db.close() if not phone_in_db: cls.new_phone = phone return phone
"""测试注册功能 自动化测试用例 要以test开头 """ import requests from common.logger_handler import get_logger logger = get_logger() def test_register_01(): """注册用例""" url = 'http://api.lemonban.com/futureloan/member/register' method = 'POST' json = {"mobile_phone": "", "pwd": ""} headers = {"X-Lemonban-Media-Type": "lemonban.v2"} expected = 2 resp = requests.request(method=method, url=url, headers=headers, json=json) # 获取响应体:json resp_body = resp.json() # 断言和日志 try: assert resp_body['code'] == expected except AssertionError as e:
class MidHandler(): """任务:中间层。 common 和 调用层。 使用项目的配置数据,填充common模块 """ # 替换数据 # 新手机号码 new_phone = '' investor_user_id = '' investor_user_token = '' admin_user_id = '' admin_user_token = '' loan_user_id = '' loan_user_token = '' # yaml 读取 yaml_path = os.path.join(path.config_path, 'config.yaml') yaml_config = read_yaml(yaml_path) user_path = os.path.join(path.config_path, 'security.yaml') user_config = read_yaml(user_path) # token turing_token_yaml_path = os.path.join(path.config_path, 'turing_token.yaml') # logger # logger_file = os.path.join(path.logs_path, yaml_config['logger']['file'] + '_' + time_now + '.log') # logger = get_logger(name=yaml_config['logger']['name'], # file=logger_file) folder_name = os.path.dirname(os.path.dirname(__file__)) folder_base_name = os.path.basename(folder_name) log_path = path.logs_path logger_file = '{}/{}_{}.log'.format(log_path, folder_base_name, time_now) logger = get_logger(name=yaml_config['logger']['name'], file=logger_file, logger_level=yaml_config['logger']['logger_level'], stream_handler_level=yaml_config['logger']['stream_handler_level'], file_handler_level=yaml_config['logger']['file_handler_level'], fmt_str=yaml_config['logger']['fmt_str'] ) # logger.debug('debug message') # excel对象 excel_file = os.path.join(path.data_path, 'cases.xlsx') excel = ExcelHandler(excel_file) # 辅助函数 # help_funcs = helper # 数据库 db_class = MidDBHandler # 需要动态替换#...# 的数据 investor_phone = user_config['investor_user']['phone'] investor_pwd = user_config['investor_user']['pwd'] loan_phone = user_config['loan_user']['phone'] loan_pwd = user_config['loan_user']['pwd'] admin_phone = user_config['admin_user']['phone'] admin_pwd = user_config['admin_user']['pwd'] # 类方法,前面用cls,代表类本身 @classmethod def replace_data(cls, my_string, pattern='#(.*?)#'): """数据动态替换""" # pattern = '#(.*?)#' results = re.finditer(pattern=pattern, string=my_string) for result in results: # old= '#investor_phone#' old = result.group() # key = 'investor_phone' key = result.group(1) if old == "#new_phone#": new = MidHandler.generate_new_phone() else: new = str(getattr(cls, key, '')) my_string = my_string.replace(old, new) return my_string @classmethod def get_token(cls): data = MidHandler.excel.read('login') url = MidHandler.yaml_config['host'] content_type = eval(data[0]['paras'])['Content-Type'] ak = MidHandler.user_config['ak'] uid = MidHandler.user_config['uid'] parameter = json.dumps({"ak": ak, "uid": uid, "token": "", "asr": 4, "tts": 3, "tone": 20}) payload = {'Content-Type': content_type, 'parameters': parameter} files = [ ('speech', ('apple.opus', open('E:/Hannto/Automation/Github/Chameleon/Audio/apple.opus', 'rb'), 'application/octet-stream')) ] headers = {} response = requests.request("POST", url, headers=headers, data=payload, files=files) res_token = response.json()['token'] rec_token = {'token': res_token} write_yaml(MidHandler.turing_token_yaml_path, rec_token) return res_token @classmethod def update_tests_data(cls): data = MidHandler.excel.read('login') case_range = len(data) index_num: int new_token = MidHandler.get_token() for index_num in range(case_range): new_parameters = eval(eval(data[index_num]['paras'])['parameters']) new_parameters["ak"] = MidHandler.user_config['ak'] new_parameters["uid"] = MidHandler.user_config['uid'] new_parameters["token"] = new_token new_paras = {"parameters": json.dumps(new_parameters)} str_new_paras = json.dumps(new_paras) data[index_num]['paras'] = str_new_paras return data @classmethod def generate_new_phone(cls): """自动生成手机号""" fk = Faker(locale='zh_CN') while True: phone = fk.phone_number() db = MidDBHandler() phone_in_db = db.query('SELECT * FROM member WHERE mobile_phone = {}'.format(phone)) db.close() if not phone_in_db: cls.new_phone = phone return phone
class LccHandler(): '''任务:中间层。common和调用层,隔离代码,使得common模块更加通用,使用common里面的代码更加简单。 使用项目的配置数据,填充common模块 ''' #替换数据 #新手机号码 new_phone = ' ' investor_member_id = '' investor_token = ' ' admin_member_id = ' ' admin_token = ' ' loan_id = ' ' loan_token = ' ' yaml_path = os.path.join(path.GetPath('config'), 'config.yaml') yaml_config = read_yaml(yaml_path) user_path = os.path.join(path.GetPath('config'), 'security.yaml') user_config = read_yaml(user_path) #logger logger_file = os.path.join(path.GetPath('logs'), yaml_config['logger']['file']) logger = get_logger(name=yaml_config['logger']['name'], file=logger_file) #excel对象 excel_file = os.path.join(path.GetPath('data'), 'cekai_cases.xlsx') excel = ExcelHandler(excel_file) #辅助函数,导入模块,其实就是重命名,把helper保存到help_funcs里面作为类属性 # help_funcs=helper #数据库 # db=DBHandle( # host=user_config['db']['host'], # port=user_config['db']['port'], # user=user_config['db']['user'], # password=user_config['db']['password'], # charset=user_config['db']['charset'], # database=user_config['db']['database'], # cursorclass=DictCursor # ) db = MidDBHandler() # new_phone=' ' @classmethod def generate_new_phone(cls): # '''自动生成手机号码''' fk = Faker(locale="zh_CN") while True: phone = fk.phone_number() db = DBHandle(host='8.129.91.152', port=3306, user='******', password='******', charset='utf8', database='futureloan', cursorclass=DictCursor) phone_in_db = db.query( 'select * from member where mobile_phone={};'.format(phone)) db.close() if not phone_in_db: cls.new_phone = phone return phone #需要动态替换#...#的数据 username = user_config['user_register']['username'] email = user_config['user_register']['email'] pwd = user_config['user_register']['pwd'] user_id = user_config['user_register']['user_id'] @classmethod def replace_data(cls, string, pattern='#(.*?)#'): """数据动态替换""" #pattern='#(.*?)#' results = re.finditer(pattern=pattern, string=string) for result in results: old = result.group() key = result.group(1) new = str(getattr(cls, key, '')) string = string.replace(old, new) return string
class YZHandler(): """任务:中间层。 common 和 调用层。 使用项目的配置数据,填充common模块 """ # 替换数据 # 新手机号码 new_phone = '' investor_user_id = '' investor_user_token = '' admin_user_id = '' admin_user_token = '' loan_user_id = '' loan_user_token = '' yaml_path = os.path.join(path.config_path, 'config.yaml') yaml_config = read_yaml(yaml_path) user_path = os.path.join(path.config_path, 'security.yaml') user_config = read_yaml(user_path) # logger logger_file = os.path.join(path.logs_path, yaml_config['logger']['file']) logger = get_logger(name=yaml_config['logger']['name'], file=logger_file) # excel对象 excel_file = os.path.join(path.data_path, 'cases.xlsx') excel = ExcelHandler(excel_file) # 辅助函数 # help_funcs = helper # 数据库 db_class = MidDBHandler # 需要动态替换#...# 的数据 investor_phone = user_config['investor_user']['phone'] investor_pwd = user_config['investor_user']['pwd'] loan_phone = user_config['loan_user']['phone'] loan_pwd = user_config['loan_user']['pwd'] admin_phone = user_config['admin_user']['phone'] admin_pwd = user_config['admin_user']['pwd'] @classmethod def replace_data(cls, string, pattern='#(.*?)#'): """数据动态替换""" # pattern = '#(.*?)#' results = re.finditer(pattern=pattern, string=string) for result in results: # old= '#investor_phone#' old = result.group() # key = 'investor_phone' key = result.group(1) new = str(getattr(cls, key, '')) string = string.replace(old, new) return string @classmethod def generate_new_phone(cls): """自动生成手机号""" fk = Faker(locale='zh_CN') while True: phone = fk.phone_number() db = MidDBHandler() phone_in_db = db.query('SELECT * FROM member WHERE mobile_phone = {}'.format(phone)) db.close() if not phone_in_db: cls.new_phone = phone return phone
class MidHandler: """ 中间层。common和调用的中间层。 使用项目的配置数据, 填充common模块。 作用: 1. 隔离代码,让common更通用 2. 使用common代码更加简单,少调用 """ # 获取配置文件中的数据 conf_data = read_yaml(os.path.join(CONF_DIR, "conf.yaml")) # 获取账户信息 security_data = read_yaml(os.path.join(CONF_DIR, "security.yaml")) # 初始化log log = get_logger(file=os.path.join(LOG_DIR, conf_data["LOG"]["FILENAME"]), name=conf_data["LOG"]["NAME"], level=conf_data["LOG"]["LEVEL"], handler_level=conf_data["LOG"]["HANDLER_LEVEL"], file_level=conf_data["LOG"]["FILE_LEVEL"], fmt=conf_data["LOG"]["FMT"]) # excel对象 # 获取excel的路径 excel_file = os.path.join(DATA_DIR, "case.xlsx") excel = ExcelHandler(excel_file) """ # 数据库 db = DBHandler(host = security_data["MYSQL"]["HOST"], port = security_data["MYSQL"]["PORT"], user = security_data["MYSQL"]["USER"], password = security_data["MYSQL"]["PWD"], database = security_data["MYSQL"]["NAME"] ) """ # 数据库 下面这种写法是重命名 db_class = MidDBHandler # --- 需要动态替换的数据 --- # 新手机号码 new_phone = "" # 投资人信息 investor_member_id = "" investor_token = "" investor_phone = security_data["investor_phone"] investor_pwd = security_data["investor_pwd"] # 借款人信息 loan_member_id = "" loan_token = "" loan_phone = security_data["loan_phone"] loan_pwd = security_data["loan_pwd"] # 管理员信息 admin_member_id = "" admin_token = "" admin_phone = security_data["admin_phone"] admin_pwd = security_data["admin_pwd"] # --- ---- --- @classmethod def replace_data(cls, string, pattern=r"#(.*?)#"): """ 动态替换数据的方法 :param string: 需要替换的字符串 :param pattern: 正则表达式匹配规则 :return: 替换后的字符串 """ res = re.finditer(pattern=pattern, string=string) for i in res: string = string.replace(i.group(), str(getattr(cls, i.group(1)))) return string @classmethod def generate_new_phone(cls): """ 获取一个数据库中未注册过的手机号码 :return: 未注册的手机号码 """ while True: phone = helper.generate_phone() db = MidDBHandler() result = db.query_all("SELECT mobile_phone from member where mobile_phone={};".format(phone)) db.close() if not result: cls.new_phone = phone return phone