refactor(): 优化文件读取,变量替换等

- 优化用例加载模块器
- 新增JSON文件读取模块
This commit is contained in:
2025-03-09 17:23:25 +08:00
parent 914b0301ba
commit a50e00a4e1
15 changed files with 219 additions and 451 deletions

View File

@@ -13,21 +13,19 @@ import copy
import json
import logging
import re
import jsonpath
import allure
from commons.templates import Template
import jsonpath
from commons.file_processors.yaml_processor import YamlProcessor, StringOrDict
from commons.models import CaseInfo
from commons.file_processors.file_handle import FileHandle
logger = logging.getLogger(__name__)
class Exchange:
def __init__(self, path):
self.file = YamlProcessor(path)
self.file = FileHandle(path)
@allure.step("提取变量")
def extract(self, resp, var_name, attr, expr: str, index):
@@ -59,29 +57,17 @@ 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: dict) -> dict:
logger.info(case_info)
logger.info(f"变量替换:{case_info}")
# 1将case_info转换为字符串
case_info_str = YamlProcessor.to_string(case_info)
case_info_str = FileHandle.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 = YamlProcessor.to_dict(case_info_str)
new_case_info = FileHandle.to_dict(case_info_str)
return new_case_info
@@ -97,7 +83,7 @@ if __name__ == '__main__':
# print(mock_resp.text)
# print(mock_resp.json())
exchanger = Exchange(r"D:\CNWei\CNW\InterfaceAutoTest\extract.yaml")
exchanger = Exchange(r"E:\PyP\InterfaceAutoTest\extract.yaml")
exchanger.extract(mock_resp, "name", "json", '$.name', 0)
exchanger.extract(mock_resp, "age", "json", '$.age', 0)
exchanger.extract(mock_resp, "data", "json", '$.data', 0)