feat(): 优化项目
- 更新README - 修复bug
This commit is contained in:
@@ -24,8 +24,8 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
session = Session(settings.base_url)
|
||||
|
||||
|
||||
_case_path = Path(settings.case_path)
|
||||
|
||||
exchanger = Exchange(settings.exchanger)
|
||||
|
||||
|
||||
@@ -41,23 +41,24 @@ class TestAPI:
|
||||
"""
|
||||
yaml_path_list = case_path.glob("**/test_*.yaml") # 搜索当前目录及其子目录下以test_开头yaml为后缀的文件
|
||||
for yaml_path in yaml_path_list:
|
||||
logger.info(f"load file {yaml_path=}")
|
||||
# logger.info(f"load file {yaml_path=}")
|
||||
|
||||
file = YamlFile(yaml_path) # 自动读取yaml文件
|
||||
case_info = CaseInfo(**file) # 校验yaml格式
|
||||
|
||||
logger.debug(f"case_info={case_info.to_yaml()}") # 把case_info 转成字符串,然后记录日志
|
||||
# logger.info(f"case_info={case_info.to_yaml()}") # 把case_info 转成字符串,然后记录日志
|
||||
|
||||
case_func = cls.new_case(case_info) # 从yaml格式转换为pytest格式
|
||||
print(yaml_path.name)
|
||||
setattr(cls, f"{yaml_path.name}", case_func) # 把pytest格式添加到类中
|
||||
print(yaml_path.stem)
|
||||
setattr(cls, f"{yaml_path.stem}", case_func) # 把pytest格式添加到类中
|
||||
|
||||
@classmethod
|
||||
def new_case(cls, case_info: CaseInfo):
|
||||
ddt_data = case_info.ddt()
|
||||
print(ddt_data)
|
||||
ddt_title = [data.title for data in ddt_data]
|
||||
|
||||
ddt_title = [data.title for data in ddt_data]
|
||||
logger.info(f"{ddt_title=}")
|
||||
@allure.feature(case_info.feature)
|
||||
@allure.story(case_info.story)
|
||||
@pytest.mark.parametrize("case_info", ddt_data, ids=ddt_title)
|
||||
|
||||
@@ -31,15 +31,16 @@ class Exchange:
|
||||
|
||||
@allure.step("提取变量")
|
||||
def extract(self, resp, var_name, attr, expr: str, index):
|
||||
# resp中json是方法不是属性,需要手动更改为属性
|
||||
|
||||
resp = copy.deepcopy(resp)
|
||||
|
||||
try:
|
||||
# resp中json是方法不是属性,需要手动更改为属性
|
||||
resp.json = resp.json()
|
||||
except json.decoder.JSONDecodeError:
|
||||
resp.json = {"msg": "is not json data"}
|
||||
|
||||
data = getattr(resp, attr)
|
||||
# print(data)
|
||||
if expr.startswith("/"): # xpath
|
||||
res = None
|
||||
elif expr.startswith("$"): # jsonpath
|
||||
@@ -53,18 +54,20 @@ class Exchange:
|
||||
else: # 如果没有数据
|
||||
value = "not data"
|
||||
|
||||
logger.debug(f"{var_name} = {value}") # 记录变量名和变量值
|
||||
logger.debug(f"{var_name} = {value}") # 记录变量名和变量值
|
||||
|
||||
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
|
||||
@@ -82,7 +85,7 @@ 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)
|
||||
|
||||
@@ -72,6 +72,7 @@ class CaseInfo:
|
||||
def ddt(self) -> list: # 返回一个列表,列表中应该包含N个注入了变量的caseInfo
|
||||
case_list = []
|
||||
if not self.parametrize: # 没有使用数据驱动测试
|
||||
logger.info("1,执行这一步")
|
||||
case_list.append('')
|
||||
else: # 使用数据驱动测试
|
||||
args_name = self.parametrize[0]
|
||||
|
||||
@@ -9,15 +9,19 @@
|
||||
@date: 2025/2/23 21:34
|
||||
@desc:
|
||||
"""
|
||||
base_url = 'http://127.0.0.1:8000'
|
||||
case_path = r"E:\PyP\InterfaceAutoTest\TestCases"
|
||||
exchanger = r"E:\PyP\InterfaceAutoTest\extract.yaml"
|
||||
id_path =r"E:\PyP\InterfaceAutoTest\id.yaml"
|
||||
from pathlib import Path
|
||||
|
||||
db_host = '119.91.19.171' # ip
|
||||
root_path = (Path(__file__)).resolve().parents[1]
|
||||
|
||||
base_url = 'http://127.0.0.1:40065'
|
||||
case_path = rf"{root_path}\TestCases\answer"
|
||||
exchanger = rf"{root_path}\extract.yaml"
|
||||
id_path = rf"{root_path}\id.yaml"
|
||||
|
||||
db_host = '127.0.0.1' # ip
|
||||
db_port = 3306 # 端口
|
||||
db_user = 'root' # 用户名
|
||||
db_password = 'mysql_hNahSe' # 密码
|
||||
db_password = 'password' # 密码
|
||||
db_database = 'answer' # 库名
|
||||
|
||||
allure_epic: str = "项目名称:answer"
|
||||
@@ -26,3 +30,7 @@ allure_story: str = "默认事件(story)"
|
||||
|
||||
rsa_public = ""
|
||||
rsa_private = ""
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(root_path)
|
||||
Reference in New Issue
Block a user