feat,fix(): 优化项目
- 优化yaml_processor(优化文件类型转换逻辑) - 修复bug
This commit is contained in:
@@ -85,20 +85,21 @@ class TestAPI:
|
|||||||
item.get(case_key)
|
item.get(case_key)
|
||||||
logger.info(f"========:{item}")
|
logger.info(f"========:{item}")
|
||||||
logger.info(f"========:{item.get(case_key)}")
|
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,变量替换
|
# 0,变量替换
|
||||||
new_case_info = exchanger.replace(case_info)
|
new_case_info = exchanger.replace(case_info)
|
||||||
logger.info(f"1,正在注入变量...")
|
logger.info(f"1,正在注入变量...")
|
||||||
|
logger.info(f"new_case_info={new_case_info}")
|
||||||
# 1,发送请求
|
# 1,发送请求
|
||||||
logger.info(f"2,正在请求接口...")
|
logger.info(f"2,正在请求接口...")
|
||||||
resp = session.request(**new_case_info.request)
|
resp = session.request(**new_case_info.get("request"))
|
||||||
|
|
||||||
logger.info(f"3,正在提取变量...")
|
logger.info(f"3,正在提取变量...")
|
||||||
# 2,保存变量(接口关联)
|
# 2,保存变量(接口关联)
|
||||||
|
new_case_info = CaseInfo(**new_case_info)
|
||||||
for var_name, extract_info in new_case_info.extract.items():
|
for var_name, extract_info in new_case_info.extract.items():
|
||||||
# logger.info(f"保存变量:{var_name}{extract_info}")
|
# logger.info(f"保存变量:{var_name}{extract_info}")
|
||||||
exchanger.extract(resp, var_name, *extract_info)
|
exchanger.extract(resp, var_name, *extract_info)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import allure
|
|||||||
from commons.templates import Template
|
from commons.templates import Template
|
||||||
import jsonpath
|
import jsonpath
|
||||||
|
|
||||||
from commons.file_processors.yaml_processor import YamlFile
|
from commons.file_processors.yaml_processor import YamlFile,StringOrDict
|
||||||
from commons.models import CaseInfo
|
from commons.models import CaseInfo
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -59,20 +59,31 @@ class Exchange:
|
|||||||
self.file[var_name] = value # 保存变量
|
self.file[var_name] = value # 保存变量
|
||||||
self.file.save() # 持久化存储到文件
|
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("替换变量")
|
@allure.step("替换变量")
|
||||||
def replace(self, case_info: CaseInfo):
|
def replace(self, case_info: dict):
|
||||||
logger.info(case_info)
|
logger.info(case_info)
|
||||||
# 1,将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=}")
|
print(f"{case_info_str=}")
|
||||||
# 2,替换字符串
|
# 2,替换字符串
|
||||||
case_info_str = Template(case_info_str).render(self.file)
|
case_info_str = Template(case_info_str).render(self.file)
|
||||||
print(f"{case_info_str=}")
|
print(f"{case_info_str=}")
|
||||||
# 3,将字符串转换成case_info
|
# 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
|
return new_case_info
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
class MockResponse:
|
class MockResponse:
|
||||||
text = '{"name":"张三","age":"18","data":[3,4,5],"aaa":null}'
|
text = '{"name":"张三","age":"18","data":[3,4,5],"aaa":null}'
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ class StringOrDict:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_dict(cls, data: str) -> None:
|
def to_dict(cls, data: str) -> Union[None, dict]:
|
||||||
"""
|
"""
|
||||||
将 YAML 格式的字符串转换为字典,并更新当前字典的内容.
|
将 YAML 格式的字符串转换为字典,并更新当前字典的内容.
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ class Session(requests.Session):
|
|||||||
|
|
||||||
logger.info(f"接收响应 <<<<<< 状态码 = {resp.status_code}")
|
logger.info(f"接收响应 <<<<<< 状态码 = {resp.status_code}")
|
||||||
logger.info(f"接收响应 <<<<<< 响应头 = {resp.headers}")
|
logger.info(f"接收响应 <<<<<< 响应头 = {resp.headers}")
|
||||||
logger.info(f"接收响应 <<<<<< 响应正文 = {resp.json()}")
|
logger.info(f"接收响应 <<<<<< 响应正文 = {resp.text}")
|
||||||
|
# logger.info(f"接收响应 <<<<<< 响应正文 = {resp.json()}")
|
||||||
|
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user