feat,fix(): 优化项目

- 优化yaml_processor(优化文件类型转换逻辑)
- 修复bug
This commit is contained in:
2025-03-07 17:28:41 +08:00
parent a6996ed500
commit 914b0301ba
9 changed files with 419 additions and 109 deletions

View File

@@ -19,7 +19,7 @@ import allure
from commons.templates import Template
import jsonpath
from commons.file_processors.yaml_processor import YamlFile,StringOrDict
from commons.file_processors.yaml_processor import YamlProcessor, StringOrDict
from commons.models import CaseInfo
logger = logging.getLogger(__name__)
@@ -27,7 +27,7 @@ logger = logging.getLogger(__name__)
class Exchange:
def __init__(self, path):
self.file = YamlFile(path)
self.file = YamlProcessor(path)
@allure.step("提取变量")
def extract(self, resp, var_name, attr, expr: str, index):
@@ -72,18 +72,19 @@ class Exchange:
# new_case_info = case_info.by_yaml(case_info_str)
# return new_case_info
@allure.step("替换变量")
def replace(self, case_info: dict):
def replace(self, case_info: dict) -> dict:
logger.info(case_info)
# 1将case_info转换为字符串
case_info_str = StringOrDict().to_string(case_info)
case_info_str = YamlProcessor.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 = StringOrDict().to_dict(case_info_str)
new_case_info = YamlProcessor.to_dict(case_info_str)
return new_case_info
if __name__ == '__main__':
class MockResponse:
text = '{"name":"张三","age":"18","data":[3,4,5],"aaa":null}'
@@ -96,19 +97,29 @@ if __name__ == '__main__':
# print(mock_resp.text)
# print(mock_resp.json())
exchanger = Exchange(r"E:\PyP\InterfaceAutoTest\extract.yaml")
exchanger = Exchange(r"D:\CNWei\CNW\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)
exchanger.extract(mock_resp, "aaa", "json", '$.aaa', 0)
mock_case_info = CaseInfo(
title="单元测试",
request={
# mock_case_info = CaseInfo(
# title="单元测试",
# request={
# "data":
# {"name": "${name}", "age": "${str(age)}", "time": "${add(1,2)}"}
# },
# extract={},
# validate={}
# )
mock_case_info = {
"title": "单元测试",
"request": {
"data":
{"name": "${name}", "age": "${str(age)}", "time": "${add(1,2)}"}
},
extract={},
validate={}
)
"extract": {},
"validate": {}
}
new_mock_case_info = exchanger.replace(mock_case_info)
print(new_mock_case_info)