Files
apiautotest/test_case/conftest.py
2025-12-25 15:48:21 +08:00

107 lines
4.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
# @Time : 2023/3/28 23:17
# @Author : Flora.Chen
# @File : conftest.py
# @Software: PyCharm
# @Desc:
# 标准库导入
import os
# 第三方库导入
import pytest
import allure
from loguru import logger
# 本地应用/模块导入
from settings import GLOBAL_VARS, GITLINK_DIR
from custom_utils.report_utils.allure_handle import allure_title
from custom_utils.requests_utils.request_control import RequestControl
@pytest.fixture(scope="function", autouse=True)
def case_control(request):
"""用例控制"""
# 使用 request.getfixturevalue() 方法来获取测试用例函数的参数值
# 注意这里的"case"需要与@pytest.mark.parametrize("case", cases)中传递的保持一致
case = request.getfixturevalue("case")
logger.info(f'\n-----------------------------START-开始执行用例- {case.get("id")} || {case.get("title")}-----------------------------')
# 添加用例标题作为allure中显示的用例标题
allure_title(case.get("title", ""))
if case.get("run") is None or case.get("run") is False:
reason = f"{case.get('id')} || {case.get('title')}: 标记了该用例不执行run=False"
logger.warning(f"{reason}")
pytest.skip(reason)
yield
logger.info("-----------------------------END-用例执行完成-----------------------------")
def pytest_collection_modifyitems(config, items):
for item in items:
# 注意这里的"case"需要与@pytest.mark.parametrize("case", cases)中传递的保持一致
parameters = item.callspec.params["case"]
# print(f"测试参数:{type(parameters)} {parameters}")
if parameters.get("severity"):
if parameters["severity"].upper() == "TRIVIAL":
item.add_marker(allure.severity(allure.severity_level.TRIVIAL))
elif parameters["severity"].upper() == "MINOR":
item.add_marker(allure.severity(allure.severity_level.MINOR))
elif parameters["severity"].upper() == "CRITICAL":
item.add_marker(allure.severity(allure.severity_level.CRITICAL))
elif parameters["severity"].upper() == "BLOCKER":
item.add_marker(allure.severity(allure.severity_level.BLOCKER))
else:
item.add_marker(allure.severity(allure.severity_level.NORMAL))
else:
item.add_marker(allure.severity(allure.severity_level.NORMAL))
@pytest.fixture(scope="session")
def gitlink_login():
"""
获取登录的cookie
:return:
"""
# 请求登录接口
res = RequestControl().api_request_flow(api_file_path=os.path.join(GITLINK_DIR, "test_api_account_login.yaml"),
key="gitlink_login_01", global_var=GLOBAL_VARS)
GLOBAL_VARS.update(res)
# @pytest.fixture(scope="session", autouse=True)
# def init_data():
# """
# 运行测试之前,初始化数据
# """
# logger.info("Start ----- 开始初始化数据...")
# with (allure.step("平台管理员登录")):
# admin_cookies = RequestControl().api_request_flow(
# api_file_path=os.path.join(GITLINK_DIR, "test_gitlink_login.yaml"),
# key="gitlink_super_login_04", global_var=GLOBAL_VARS)
# GLOBAL_VARS.update(admin_cookies)
# GLOBAL_VARS.update({"cookies": admin_cookies["admin_cookies"]})
#
# with allure.step("新建公开项目"):
# GLOBAL_VARS["user_id"] = GLOBAL_VARS["env_super_user_id"]
# GLOBAL_VARS["repo_owner"] = GLOBAL_VARS["env_super_login"]
# project_info = RequestControl().api_request_flow(
# api_file_path=os.path.join(GITLINK_DIR, "projects", "test_gitlink_new_project.yaml"),
# key="gitlink_projects_new_project_01", global_var=GLOBAL_VARS)
# GLOBAL_VARS.update(project_info)
# # 保留初始的项目拥有者和项目标识,方便测试结束后删除项目
# GLOBAL_VARS.update({"init_repo_owner": project_info["repo_owner"]})
# GLOBAL_VARS.update({"init_repo_identifier": project_info["repo_identifier"]})
# logger.info(f"项目信息:{GLOBAL_VARS}")
#
# GLOBAL_VARS.pop("user_id")
#
# logger.info("End ----- 初始化数据完成!")
# yield
# with allure.step("删除测试项目"):
# GLOBAL_VARS.update({"cookies": GLOBAL_VARS["admin_cookies"]})
# # 获取初始的项目拥有者和项目标识,重新赋值,便于删除项目
# GLOBAL_VARS.update({"repo_owner": GLOBAL_VARS["init_repo_owner"]})
# GLOBAL_VARS.update({"repo_identifier": GLOBAL_VARS["init_repo_identifier"]})
# RequestControl().api_request_flow(api_file_path=os.path.join(GITLINK_DIR, "projects",
# "test_gitlink_delete_project.yaml"),
# key="gitlink_projects_delete_project_01", global_var=GLOBAL_VARS)