修复story/feature等测试类型无法找到SZPurchase目录下文件的问题

This commit is contained in:
qiaoxinjiu
2026-05-09 15:54:35 +08:00
parent b15c4d1c31
commit 7abf76fbbd

View File

@@ -82,23 +82,27 @@ def find_test_files(directory, include_all=False):
return test_files return test_files
def get_all_test_files():
"""获取所有测试文件(包含标准 test_*.py 和 SZPurchase 目录下的所有 .py 文件)"""
test_files = find_test_files(case_dir)
szpurchase_dir = os.path.join(case_dir, '接口', 'SZPurchase')
if os.path.exists(szpurchase_dir):
szpurchase_files = find_test_files(szpurchase_dir, include_all=True)
test_files.extend(szpurchase_files)
print(f"添加 SZPurchase 目录下的 {len(szpurchase_files)} 个文件")
return test_files
def run_tests(target, test_type='all', **kwargs): def run_tests(target, test_type='all', **kwargs):
"""运行测试""" """运行测试"""
base_args = ['-v', '--tb=short', f'--alluredir={ALLURE_RESULTS_DIR}'] base_args = ['-v', '--tb=short', f'--alluredir={ALLURE_RESULTS_DIR}']
if test_type == 'all': if test_type == 'all':
print("运行所有测试用例...") print("运行所有测试用例...")
# 查找所有 test_*.py 文件 all_test_files = get_all_test_files()
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 = [] test_files = []
skipped_files = [] skipped_files = []
for file_path in all_test_files: for file_path in all_test_files:
@@ -109,7 +113,7 @@ def run_tests(target, test_type='all', **kwargs):
if skipped_files: if skipped_files:
print(f"跳过 {len(skipped_files)} 个有导入问题的文件:") print(f"跳过 {len(skipped_files)} 个有导入问题的文件:")
for f in skipped_files[:5]: # 只显示前5个 for f in skipped_files[:5]:
print(f" - {os.path.relpath(f, project_root)}") print(f" - {os.path.relpath(f, project_root)}")
if len(skipped_files) > 5: if len(skipped_files) > 5:
print(f" ... 还有 {len(skipped_files) - 5} 个文件") print(f" ... 还有 {len(skipped_files) - 5} 个文件")
@@ -120,21 +124,21 @@ def run_tests(target, test_type='all', **kwargs):
args = test_files + base_args args = test_files + base_args
elif test_type == 'feature': elif test_type == 'feature':
print(f"按feature标签运行: {target}") print(f"按feature标签运行: {target}")
test_files = find_test_files(case_dir) test_files = get_all_test_files()
if not test_files: if not test_files:
print("错误: 未找到测试文件") print("错误: 未找到测试文件")
return 1 return 1
args = test_files + [f'--allure-features={target}'] + base_args args = test_files + [f'--allure-features={target}'] + base_args
elif test_type == 'story': elif test_type == 'story':
print(f"按story标签运行: {target}") print(f"按story标签运行: {target}")
test_files = find_test_files(case_dir) test_files = get_all_test_files()
if not test_files: if not test_files:
print("错误: 未找到测试文件") print("错误: 未找到测试文件")
return 1 return 1
args = test_files + [f'--allure-stories={target}'] + base_args args = test_files + [f'--allure-stories={target}'] + base_args
elif test_type == 'marker': elif test_type == 'marker':
print(f"按pytest标记运行: {target}") print(f"按pytest标记运行: {target}")
test_files = find_test_files(case_dir) test_files = get_all_test_files()
if not test_files: if not test_files:
print("错误: 未找到测试文件") print("错误: 未找到测试文件")
return 1 return 1
@@ -145,10 +149,8 @@ def run_tests(target, test_type='all', **kwargs):
print(f"错误: 目录不存在: {full_path}") print(f"错误: 目录不存在: {full_path}")
return 1 return 1
print(f"按目录运行: {target}") print(f"按目录运行: {target}")
# 先尝试查找 test_*.py 文件
test_files = find_test_files(full_path) test_files = find_test_files(full_path)
if not test_files: if not test_files:
# 如果没有找到 test_*.py 文件,尝试包含所有 .py 文件
print("未找到 test_*.py 文件,尝试查找所有 .py 文件...") print("未找到 test_*.py 文件,尝试查找所有 .py 文件...")
test_files = find_test_files(full_path, include_all=True) test_files = find_test_files(full_path, include_all=True)
if not test_files: if not test_files:
@@ -164,7 +166,7 @@ def run_tests(target, test_type='all', **kwargs):
args = [full_path] + base_args args = [full_path] + base_args
elif test_type == 'keyword': elif test_type == 'keyword':
print(f"按关键字运行: {target}") print(f"按关键字运行: {target}")
test_files = find_test_files(case_dir) test_files = get_all_test_files()
if not test_files: if not test_files:
print("错误: 未找到测试文件") print("错误: 未找到测试文件")
return 1 return 1