代码优化

This commit is contained in:
floraachy
2025-12-25 15:48:21 +08:00
parent 0911eeccd4
commit 685e8b8ce2
17 changed files with 22 additions and 43 deletions

View File

@@ -133,7 +133,7 @@ class CaseDataCheck:
'request_type': self.get_request_type,
'payload': self.case_data.get(TestCaseEnum.PAYLOAD.value[0]),
'files': self.case_data.get(TestCaseEnum.FILES.value[0]),
"think_time": self.case_data.get(TestCaseEnum.THINK_TIME.value[0]),
"wait_seconds": self.case_data.get(TestCaseEnum.WAIT_SECONDS.value[0]),
"validate": self.case_data.get(TestCaseEnum.VALIDATE.value[0]),
'extract': self.case_data.get(TestCaseEnum.EXTRACT.value[0]),
"case_dependence": self.case_data.get(TestCaseEnum.CASE_DEPENDENCE.value[0])

View File

@@ -279,7 +279,7 @@ def gen_case_file(filename, case_template_path, config, common_dependence, case_
"func_title": func_name,
# 在测试用例中移除了测试类,直接使用测试方法
# "class_title": class_name,
"now": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
"now": datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
)

View File

@@ -12,7 +12,7 @@ import pytest
from loguru import logger
# 本地应用/模块导入
from settings import GLOBAL_VARS
from common_utils.requests_utils.case_dependence import CaseDependenceHandler
from custom_utils.requests_utils.case_dependence import CaseDependenceHandler
@pytest.fixture(scope="module", autouse=True)
def init_data():

View File

@@ -98,19 +98,6 @@ def none_to_null(target):
return target
def get_file_base64(file_name):
"""
返回文文件内容的base64编码
"""
file_path = os.path.join(FILES_DIR, file_name)
if os.path.exists(file_path):
# 如果文件是一个真实存在的路径则返回base64编码内容
return file_to_base64(file_path=file_path)
else:
logger.warning(f"找不到该文件,将返回空, 传入的文件名:{file_name}")
return None
def get_filepath_base64(file_name):
"""
返回文件路径的base64编码

View File

@@ -78,7 +78,7 @@ class TestCaseEnum(Enum):
REQUEST_TYPE = ("request_type", True)
PAYLOAD = ("payload", False)
FILES = ("files", False)
THINK_TIME = ("think_time", False)
WAIT_SECONDS = ("wait_seconds", False)
EXTRACT = ("extract", False)
VALIDATE = ("validate", True)
ASSERT_SQL = ("assert_sql", False)

View File

@@ -97,13 +97,15 @@ class CaseDependenceHandler:
self.handle_interfaces(interfaces)
else:
logger.error("依赖接口格式错误,跳过依赖接口处理~ --> interface 仅支持str和list格式")
if case_dependence.get("database") and db_info:
database_dependence = case_dependence["database"]
if isinstance(database_dependence, (dict, list)):
self.handle_database_dependence(database_dependence, db_info)
if case_dependence.get("database"):
if db_info:
database_dependence = case_dependence["database"]
if isinstance(database_dependence, (dict, list)):
self.handle_database_dependence(database_dependence, db_info)
else:
logger.error("依赖数据库格式错误,跳过依赖数据库处理~ --> database 仅支持dict和list格式")
else:
logger.error("依赖数据库格式错误,跳过依赖数据库处理~ --> database 仅支持dict和list格式")
logger.error("数据库依赖参数未传入db_info跳过依赖数据库处理~")
else:
logger.error("数据库依赖参数必须传入db_info, 以及检查关键字 database是否正确")
logger.debug("不存在关键字database,跳过依赖数据库处理~")
return self.source

View File

@@ -424,6 +424,7 @@ class RequestControl(BaseRequest):
self.api_step_record(**new_api_data)
# 进行响应断言
AssertHandle(assert_data=new_api_data["validate"], response=response).assert_handle()
# todo 数据库断言 和 响应断言二选一
# 进行响应参数提取,并保存提取后的数据
if new_api_data.get("extract"):

View File

@@ -35,7 +35,6 @@ teststeps:
expect_value: success
assert_type: ==
type_jsonpath: $.message
assert_sql:
extract:
case_dependence:
setup:

View File

@@ -30,7 +30,6 @@ teststeps:
message: 断言接口返回包括字段project_categories
expect_value: project_categories
assert_type: contains
assert_sql:
extract:
type_jsonpath:
repo_category_ids: $.project_categories[*].id

View File

@@ -25,7 +25,6 @@ teststeps:
files:
validate:
status_code: 200
assert_sql:
extract:
type_jsonpath:
ignores_id: $.ignores[0].id

View File

@@ -30,7 +30,6 @@ teststeps:
message: 断言接口返回包括字段project_languages
expect_value: project_languages
assert_type: contains
assert_sql:
extract:
type_jsonpath:
repo_language_ids: $.project_languages[*].id

View File

@@ -26,7 +26,6 @@ teststeps:
files:
validate:
status_code: 200
assert_sql:
extract:
type_jsonpath:
licenses_ids: $.licenses[*].id

View File

@@ -46,7 +46,6 @@ teststeps:
message: 断言接口返回的存在id
expect_value: id
assert_type: contains
assert_sql:
extract:
type_jsonpath:
project_id: $.id # 项目id
@@ -81,7 +80,6 @@ teststeps:
expect_value: ${login}
assert_type: ==
type_jsonpath: $.login
assert_sql:
extract:
type_jsonpath:
repo_id: $.id
@@ -132,7 +130,6 @@ teststeps:
expect_value: ${login}
assert_type: ==
type_jsonpath: $.login
assert_sql:
extract:
type_jsonpath:
repo_id: $.id

View File

@@ -30,7 +30,6 @@ teststeps:
expect_value: TOC出库订单导入模板(2).xlsx
assert_type: ==
type_jsonpath: $.title
assert_sql:
extract:
type_jsonpath:
attachment_id: $.id
@@ -61,5 +60,4 @@ teststeps:
expect_value: 请登录后再操作
assert_type: ==
type_jsonpath: $.message
assert_sql:
extract:

View File

@@ -5,7 +5,6 @@ config:
pytest_markers:
- gitlink
- smoke
- login
- login: 登录接口
teststeps:
- id: gitlink_login_01

12
run.py
View File

@@ -42,14 +42,14 @@ import pytest
from loguru import logger
import click
# 本地应用/模块导入
from common.files_utils.files_handle import load_yaml_file
from common_utils.case_generate_utils.case_fun_generate import generate_cases
from common_utils.report_utils.send_result_handle import send_result
from common_utils.files_utils.files_handle import load_yaml_file
from custom_utils.case_generate_utils.case_fun_generate import generate_cases
from custom_utils.report_utils.send_result_handle import send_result
from settings import BASE_DIR, REPORT_DIR, LOG_DIR, ENV_DIR, ALLURE_RESULTS_DIR, ALLURE_HTML_DIR, AUTO_CASE_DIR, \
ALLURE_CONFIG_DIR, LIB_DIR
ALLURE_CONFIG_DIR
from settings import LOG_LEVEL, GLOBAL_VARS, REPORT, RERUN, RERUN_DELAY, MAX_FAIL
from common.logger_utils.loguru_log import capture_logs
from common_utils.report_utils.allure_handle import generate_allure_report
from common_utils.logger_utils.loguru_log import capture_logs
from custom_utils.report_utils.allure_handle import generate_allure_report
# from common_utils.report_utils.push_allure_report import push_allure_report

View File

@@ -14,8 +14,8 @@ import allure
from loguru import logger
# 本地应用/模块导入
from settings import GLOBAL_VARS, GITLINK_DIR
from common_utils.report_utils.allure_handle import allure_title
from common_utils.requests_utils.request_control import RequestControl
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)