def query_high_risk_diabetes(): ''' 查询糖尿病高危第一个人id :return: ''' sql = "select id as id" \ "residenter_id as residenterId from biz_ph_high_risk_group_diabetes_info where active = '1' limit 1;" rst = query_db(sql) return rst[0]
def submit_data_and_verify(username, pwd, uri, body, expected_rst, sql_path, yml_path, send_method, **params): ''' 提交数据请求后,验证数据库结果 :param expected_rst: 预期结果 :param url: 请求url :param body: 请求报文 :param sql_path: sql文件 :param yml_path: yml格式化文件 :param send_method: 请求方法 :return: ''' sql_file = sql_path yml_file = yml_path # 随机函数格式化body、conditions、expected_rst # 格式化前置条件传的数据 setUp_dict = params['setUp'] body = format_data_by_setup(body, setUp_dict) conditions = format_data_by_setup(params['query'], setUp_dict) expected_rst = format_data_by_setup(expected_rst, setUp_dict) random_obj = RandomData() expected_rst = random_obj.get_random_data_value(expected_rst) # 登录 lg = Login(username, pwd) req = lg.req headers = { "Authorization": 'Bearer ' + lg.access_token, "k2": lg.defaultLoc, "k1": lg.defaultIns } # 发送接口请求 url = URL_INFO['TEST']['url'] + uri if send_method == 'POST': rst = req.send_post(url, data=body, headers=headers) else: rst = req.send_get(url, data=body, headers=headers) # 数据库查询校验 # 如果yml配置文件为空,则直接查询数据库,不需要进行格式化 if yml_path == '': db_rst = query_db(sql_file, query=conditions) logger('数据库查询结果:').debug(db_rst) if len(db_rst) > 0: fm_db_rst = json.dumps(db_rst[0]) else: logger('数据库查询结果:').error('数据库查询结果为空') fm_db_rst = '{}' else: fm_db_rst = get_db_format_data(sql_file, yml_file, query=conditions) logger('数据库查询结果根据YML格式化为:').debug(fm_db_rst) # 数据库查询结果和预期结果比对 logger('【=====expected_rst预期结果为:=====】').debug(expected_rst) logger('【=====数据库需比对数据:=====】').debug(fm_db_rst) compare_data(expected_rst, fm_db_rst)
def scene_middle_verify(username, pwd, scene_data, setUp=None): ''' 场景类自动化用例验证 :param username: 登录用户名 :param pwd: 登陆密码 :param scene_data: 场景类的驱动数据 :param setUp: 前置条件返回的结果数据 :return: ''' lg = Login(username, pwd) req = lg.req headers = { "Authorization": 'Bearer ' + lg.access_token, "k2": lg.defaultLoc, "k1": lg.defaultIns } # 将接口依赖关系存存入dict interface_relations = {} for interface_data in scene_data: interface_relations[interface_data['uri']] = interface_data['rely'] # 解析接口依赖关系,梳理接口依赖关系;决定接口执行顺序,(场景接口数据根据接口依赖重新排序) scene_data_new = [] for interface, rely_interface in interface_relations.items(): for scene in scene_data: if rely_interface == "" and rely_interface == scene['rely']: scene_data_new.append(scene) for scene_new in scene_data_new: for scene in scene_data: if scene['rely'] == scene_new['uri']: scene_data_new.append(scene) # 然后针对每个接口发送请求,根据接口依赖取值,并保存;提供给其他依赖与此接口的返回值; # 接口返回结果 reponse_rst_dict = {} for index, interface_data in enumerate(scene_data_new): # 发送接口请求 uri = interface_data['uri'] # 根据场景的前置条件格式化body body = format_data_by_setup(interface_data['body'], setUp) if interface_data['rely']: # 获取依赖的数据,并格式化body,将依赖的数据写入填充到body里面 body = format_body_by_rely( interface_data['body'], reponse_rst_dict[interface_data['rely']]) # 打印body日志 logger('【接口' + str(index + 1) + '】请求报文body为:').debug(body) if isinstance(body, str): body = json.loads(body) url = URL_INFO['TEST']['url'] + uri if interface_data['send_method'] == 'POST': rst = req.send_post(url, data=body, headers=headers) else: rst = req.send_get(url, data=body, headers=headers) # 打印接口返回结果日志 logger('【接口' + str(index + 1) + '】返回结果为:').debug(rst) # 如果SQL执行字段为空,则保存接口返回结果; 否则查询数据库结果保存 if interface_data['sql'] and index < len(scene_data_new) - 1: sql_file = interface_data['sql'] condition = json.loads(interface_data['condition']) db_rst = query_db(sql_file, query=condition) reponse_rst_dict[uri] = db_rst[0] else: if rst: reponse_rst_dict[uri] = rst else: logger(uri).error('接口返回结果错误!') raise AssertionError(uri, '接口返回结果错误') # 最后一个接口结束后,验证场景结果 # 如果最后一个接口是查询类,则写SQL查询校验 if scene_data_new[-1]['interface_type'] == 0: rst = reponse_rst_dict[scene_data_new[-1]['uri']] yml_file = scene_data_new[-1]['yml'] sql_file = scene_data_new[-1]['sql'] expected_rst = scene_data_new[-1]['expected_rst'] # condition = json.loads(scene_data_new[-1]['condition']) condition = json.loads( format_body_by_rely(scene_data_new[-1]['condition'], reponse_rst_dict[interface_data['rely']])) target_json = format_data(rst, yml_file) logger('接口返回结果根据YML格式化为:').debug(target_json) # 如果expected_rst 期望结果不为空,则直接expected_rst与target_json比对; if expected_rst: compare_data(target_json, expected_rst) else: # 查询数据库,执行数据库查询并返回格式化后的结果 fm_db_rst = get_db_format_data(sql_file, yml_file, query=condition) logger('数据库查询结果根据YML格式化为:').debug(fm_db_rst) # 数据库查询结果和接口返回结果比对 logger('【=====接口需比对数据:=====】').debug(target_json) logger('【=====数据库需比对数据:=====】').debug(fm_db_rst) compare_data(target_json, fm_db_rst) # 如果最后一个接口是提交类,则直接执行SQL查询结果 else: # 数据库查询校验 expected_rst = scene_data_new[-1]['expected_rst'] yml_file = scene_data_new[-1]['yml'] sql_file = scene_data_new[-1]['sql'] # condition = json.loads(scene_data_new[-1]['condition']) condition = json.loads( format_body_by_rely(scene_data_new[-1]['condition'], reponse_rst_dict[interface_data['rely']])) if yml_file == '': db_rst = query_db(sql_file, query=condition) logger('数据库查询结果:').debug(db_rst) if len(db_rst) > 0: fm_db_rst = json.dumps(db_rst[0]) else: logger('数据库查询结果:').error('数据库查询结果为空') fm_db_rst = '{}' else: fm_db_rst = get_db_format_data(sql_file, yml_file, query=condition) logger('数据库查询结果根据YML格式化为:').debug(fm_db_rst) # 数据库查询结果和预期结果比对 logger('【=====expected_rst预期结果为:=====】').debug(expected_rst) logger('【=====数据库需比对数据:=====】').debug(fm_db_rst) compare_data(expected_rst, fm_db_rst)