def read_config(cls, expr: str = '.') -> dict: """ 默认读取config目录下config.yaml配置文件,根据传递的expr jsonpath表达式可任意返回配置项 :param expr: 提取表达式,使用jsonpath语法,默认值提取整个读取的对象 :return: 根据表达式返回的值 """ return extractor(cls.get_config_dict(), expr)
def assert_result(cls, response: dict, expect_str: str): """ 预期结果实际结果断言方法 :param response: 实际响应字典 :param expect_str: 预期响应内容,从excel中读取 return None """ expect_dict = convert_json(expect_str) index = 0 for k, v in expect_dict.items(): actual = extractor(response, k) index += 1 logger.info( f'第{index}个断言,实际结果:{actual} | 预期结果:{v} \n断言结果 {actual == v}') allure_step(f'第{index}个断言', f'实际结果:{actual} = 预期结果:{v}') assert actual == v
class BaseRequest(object): session = None @classmethod def get_session(cls): if cls.session is None: cls.session = requests.Session() return cls.session @classmethod def send_request(cls, case: list, env: str = 'dev') -> object: """处理case数据,转换成可用数据发送请求 :param case: 读取出来的每一行用例内容,可进行解包 :param env: 环境名称 默认使用config.yaml server下的 dev 后面的基准地址 return: 响应结果, 预期结果 """ case_number, case_title, path, token, method, parametric_key, file_obj, data, sql, expect, is_save = case logger.debug( f"用例进行处理前数据: \n 接口路径: {path} \n 请求参数: {data} \n 后置sql: {sql} \n 预期结果: {expect} \n 保存响应: {is_save}" ) # allure报告 用例标题 allure_title(case_title) # 处理url、header、data、file、的前置方法 url = ReadFile.read_config( f'$.server.{env}') + DataProcess.handle_path(path) allure_step('请求地址', url) header = DataProcess.handle_header(token) allure_step('请求头', header) data = DataProcess.handle_data(data) allure_step('请求参数', data) file = DataProcess.handler_files(file_obj) allure_step('上传文件', file_obj) # 发送请求 res = cls.send_api(url, method, parametric_key, header, data, file) allure_step('响应耗时(s)', res.elapsed.total_seconds()) allure_step('响应内容', res.json()) # 响应后操作 if token == '写': DataProcess.have_token['Authorization'] = extractor( res.json(), ReadFile.read_config('$.expr.token')) allure_step('请求头中添加Token', DataProcess.have_token) # 保存用例的实际响应 if is_save == "是": DataProcess.save_response(case_number, res.json()) allure_step('存储实际响应', DataProcess.response_dict) return res.json(), expect, sql
def assert_result(cls, response: dict, expect_str: str): """ 预期结果实际结果断言方法 :param response: 实际响应结果 :param expect_str: 预期响应内容,从excel中读取 return None """ # 后置sql变量转换 expect_str = rep_expr(expect_str, DataProcess.response_dict) expect_dict = convert_json(expect_str) index = 0 for k, v in expect_dict.items(): # 获取需要断言的实际结果部分 actual = extractor(response, k) index += 1 logger.info( f'第{index}个断言,实际结果:{actual} | 预期结果:{v} \n断言结果 {actual == v}') allure_step(f'第{index}个断言', f'实际结果:{actual} = 预期结果:{v}') try: assert actual == v except AssertionError: raise AssertionError(f'断言失败 -|- 实际结果:{actual} || 预期结果: {v}')
import tools tools.logger('predictor.log') data4work = tools.extractor('predictor.log') true_slice_list = tools.true_slicer(data4work) true_chunks = tools.true_chunker(data4work) false_chunks = tools.false_chunker(data4work) for true_slice in true_slice_list: for chunk in true_slice.copy(): if chunk in false_chunks: true_slice.remove(chunk) true_chunks.remove(chunk) print(tools.find_intersection(true_slice_list))