Files
apiautotest/test_case/conftest.py
2024-10-10 13:41:58 +08:00

147 lines
6.7 KiB
Python
Raw 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 config.global_vars import GLOBAL_VARS
from config.path_config import GITLINK_DIR
from utils.report_utils.allure_handle import allure_title
from 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("\n\n-----------------------------START-开始执行用例-----------------------------\n\n")
# 添加用例标题作为allure中显示的用例标题
allure_title(case.get("title", ""))
if case.get("run") is None or case.get("run") is False:
reason = f"{case.get('title')}: 标记了该用例为false不执行\\n"
logger.warning(f"{reason}")
pytest.skip(reason)
yield
logger.info("\n\n-----------------------------END-用例执行完成-----------------------------\n\n")
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="module")
def gitlink_login():
"""
获取登录的cookie
:return:
"""
# 请求登录接口
res = RequestControl().api_request_flow(api_file_path=os.path.join(GITLINK_DIR, "test_gitlink_login.yaml"),
key="gitlink_login_01", global_var=GLOBAL_VARS)
GLOBAL_VARS.update(res)
@pytest.fixture(scope="session")
def get_oauth_token():
"""
获取oauth_token 用于在接口的headers里面传递{AuthorizationBearer {{token}}}
注意oauth_token适用于application通过客户端的方式登录平台。application不同于正常注册平台的用户是没有用户名和密码的。
"""
res = RequestControl().api_request_flow(api_file_path=os.path.join(GITLINK_DIR, "login_oauth_token.yaml"),
key="gitlink_login_oauth_token_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"]})
with allure.step("添加测试账号作为项目成员-开发者"):
GLOBAL_VARS["added_user_id"] = GLOBAL_VARS["env_user_id"]
RequestControl().api_request_flow(
api_file_path=os.path.join(GITLINK_DIR, "projects", "collaborators",
"test_gitlink_add_repo_collaborator.yaml"),
key="gitlink_add_repo_collaborator_01", global_var=GLOBAL_VARS)
with allure.step("开启数据集导航栏"):
RequestControl().api_request_flow(
api_file_path=os.path.join(GITLINK_DIR, "projects", "settings",
"test_gitlink_update_project_unit.yaml"),
key="gitlink_update_project_unit_01", global_var=GLOBAL_VARS)
with allure.step("创建数据集"):
# 获取开源许可证
licence_info = RequestControl().api_request_flow(
api_file_path=os.path.join(GITLINK_DIR, "projects",
"test_gitlink_get_repo_licenses.yaml"),
key="gitlink_get_repo_licenses_01", global_var=GLOBAL_VARS)
GLOBAL_VARS.update(licence_info)
# 创建数据集
RequestControl().api_request_flow(
api_file_path=os.path.join(GITLINK_DIR, "projects", "dataset",
"test_gitlink_repo_new_dataset.yaml"),
key="gitlink_repo_new_dataset_01", global_var=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)