Update test framework: fix run_tests.py to support all test files, add auto-import-check for test files

This commit is contained in:
qiaoxinjiu
2026-05-09 15:11:30 +08:00
parent eb053a347f
commit eaba8328da
21739 changed files with 2236758 additions and 719 deletions

View File

@@ -20,8 +20,8 @@ TEST_CASE_DIR = 'zhyy/test_case/TestCase'
case_dir = os.path.join(project_root,TEST_CASE_DIR)
# 报告目录
REPORT_DIR = os.path.join(os.path.dirname(current_file_path), 'reports')
ALLURE_RESULTS_DIR = os.path.join(project_root, 'allure-results')
ALLURE_REPORT_DIR = os.path.join(project_root, 'allure-report')
ALLURE_RESULTS_DIR = os.path.join(REPORT_DIR, 'allure-results')
ALLURE_REPORT_DIR = os.path.join(REPORT_DIR, 'allure-report')
print(ALLURE_REPORT_DIR)
def ensure_dirs():
@@ -53,13 +53,32 @@ def run_pytest(args_list):
return result.returncode
def find_test_files(directory):
def is_importable(file_path):
"""检查文件是否可以导入(用于检测依赖问题)"""
import importlib.util
try:
spec = importlib.util.spec_from_file_location("test_module", file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return True
except Exception:
return False
def find_test_files(directory, include_all=False):
"""递归查找所有测试文件"""
test_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.py') and not file.startswith('__'):
test_files.append(os.path.join(root, file))
file_path = os.path.join(root, file)
if include_all:
# 包含所有 .py 文件(用于特殊目录如 SZPurchase
test_files.append(file_path)
else:
# 只查找以 test_ 开头的 Python 文件(符合 pytest 命名约定)
if file.startswith('test_'):
test_files.append(file_path)
return test_files
@@ -69,9 +88,34 @@ def run_tests(target, test_type='all', **kwargs):
if test_type == 'all':
print("运行所有测试用例...")
test_files = find_test_files(case_dir)
# 查找所有 test_*.py 文件
all_test_files = find_test_files(case_dir)
# 添加 SZPurchase 目录下的所有 .py 文件
szpurchase_dir = os.path.join(case_dir, '接口', 'SZPurchase')
if os.path.exists(szpurchase_dir):
szpurchase_files = find_test_files(szpurchase_dir, include_all=True)
all_test_files.extend(szpurchase_files)
print(f"添加 SZPurchase 目录下的 {len(szpurchase_files)} 个文件")
# 检查每个文件是否可以导入,跳过有导入错误的文件
test_files = []
skipped_files = []
for file_path in all_test_files:
if is_importable(file_path):
test_files.append(file_path)
else:
skipped_files.append(file_path)
if skipped_files:
print(f"跳过 {len(skipped_files)} 个有导入问题的文件:")
for f in skipped_files[:5]: # 只显示前5个
print(f" - {os.path.relpath(f, project_root)}")
if len(skipped_files) > 5:
print(f" ... 还有 {len(skipped_files) - 5} 个文件")
if not test_files:
print("错误: 未找到测试文件")
print("错误: 未找到可运行的测试文件")
return 1
args = test_files + base_args
elif test_type == 'feature':
@@ -101,7 +145,12 @@ def run_tests(target, test_type='all', **kwargs):
print(f"错误: 目录不存在: {full_path}")
return 1
print(f"按目录运行: {target}")
# 先尝试查找 test_*.py 文件
test_files = find_test_files(full_path)
if not test_files:
# 如果没有找到 test_*.py 文件,尝试包含所有 .py 文件
print("未找到 test_*.py 文件,尝试查找所有 .py 文件...")
test_files = find_test_files(full_path, include_all=True)
if not test_files:
print("错误: 未找到测试文件")
return 1