diff --git a/commons/cases.py b/commons/cases.py index 6250c01..c47b1be 100644 --- a/commons/cases.py +++ b/commons/cases.py @@ -85,20 +85,21 @@ class TestAPI: item.get(case_key) logger.info(f"========:{item}") logger.info(f"========:{item.get(case_key)}") - # allure.dynamic.title(case_info.title) + allure.dynamic.title(case_info.get("title")) - logger.info(f"用例开始执行:{case_info.title}".center(80, "=")) + logger.info(f"用例开始执行:{case_info.get('title')}".center(80, "=")) # 0,变量替换 new_case_info = exchanger.replace(case_info) logger.info(f"1,正在注入变量...") - + logger.info(f"new_case_info={new_case_info}") # 1,发送请求 logger.info(f"2,正在请求接口...") - resp = session.request(**new_case_info.request) + resp = session.request(**new_case_info.get("request")) logger.info(f"3,正在提取变量...") # 2,保存变量(接口关联) + new_case_info = CaseInfo(**new_case_info) for var_name, extract_info in new_case_info.extract.items(): # logger.info(f"保存变量:{var_name}{extract_info}") exchanger.extract(resp, var_name, *extract_info) diff --git a/commons/exchange.py b/commons/exchange.py index 85b9e0d..ed3366c 100644 --- a/commons/exchange.py +++ b/commons/exchange.py @@ -19,7 +19,7 @@ import allure from commons.templates import Template import jsonpath -from commons.file_processors.yaml_processor import YamlFile +from commons.file_processors.yaml_processor import YamlFile,StringOrDict from commons.models import CaseInfo logger = logging.getLogger(__name__) @@ -59,20 +59,31 @@ class Exchange: self.file[var_name] = value # 保存变量 self.file.save() # 持久化存储到文件 + # @allure.step("替换变量") + # def replace(self, case_info: CaseInfo): + # logger.info(case_info) + # # 1,将case_info转换为字符串 + # case_info_str = case_info.to_yaml() + # print(f"{case_info_str=}") + # # 2,替换字符串 + # case_info_str = Template(case_info_str).render(self.file) + # print(f"{case_info_str=}") + # # 3,将字符串转换成case_info + # new_case_info = case_info.by_yaml(case_info_str) + # return new_case_info @allure.step("替换变量") - def replace(self, case_info: CaseInfo): + def replace(self, case_info: dict): logger.info(case_info) # 1,将case_info转换为字符串 - case_info_str = case_info.to_yaml() + case_info_str = StringOrDict().to_string(case_info) print(f"{case_info_str=}") # 2,替换字符串 case_info_str = Template(case_info_str).render(self.file) print(f"{case_info_str=}") # 3,将字符串转换成case_info - new_case_info = case_info.by_yaml(case_info_str) + new_case_info = StringOrDict().to_dict(case_info_str) return new_case_info - if __name__ == '__main__': class MockResponse: text = '{"name":"张三","age":"18","data":[3,4,5],"aaa":null}' diff --git a/commons/file_processors/yaml_processor.py b/commons/file_processors/yaml_processor.py index 9e2e74b..4b94833 100644 --- a/commons/file_processors/yaml_processor.py +++ b/commons/file_processors/yaml_processor.py @@ -136,7 +136,7 @@ class StringOrDict: return "" @classmethod - def to_dict(cls, data: str) -> None: + def to_dict(cls, data: str) -> Union[None, dict]: """ 将 YAML 格式的字符串转换为字典,并更新当前字典的内容. diff --git a/commons/session.py b/commons/session.py index ac39670..4b326b5 100644 --- a/commons/session.py +++ b/commons/session.py @@ -41,7 +41,8 @@ class Session(requests.Session): logger.info(f"接收响应 <<<<<< 状态码 = {resp.status_code}") logger.info(f"接收响应 <<<<<< 响应头 = {resp.headers}") - logger.info(f"接收响应 <<<<<< 响应正文 = {resp.json()}") + logger.info(f"接收响应 <<<<<< 响应正文 = {resp.text}") + # logger.info(f"接收响应 <<<<<< 响应正文 = {resp.json()}") return resp