Files
smart-management-auto-test/dulizhan/test_case/run_tests.py
zhouqi 3191ec4f3c feat: 新增JoyHub模块测试用例和功能增强
1. 新增模块测试用例:
   - News分类接口测试 (Joyhub_NewsCate.py)
   - News内容接口测试 (Joyhub_News.py)
   - 产品分类接口测试 (Joyhub_ProductCate.py)
   - 产品属性接口测试 (Joyhub_ProductAttr.py)
   - 产品管理接口测试 (Joyhub_Product.py)
   - FAQ分类接口测试 (Joyhub_FaqCate.py)
   - FAQ内容接口测试 (Joyhub_Faq.py)
   - 博客分类接口测试 (Joyhub_BlogCate.py)
   - 地址国家接口测试 (Joyhub_AddressCountry.py)
   - 下载二维码接口测试 (Joyhub_DownloadQrcode.py)
   - 支付页产品推荐接口测试 (Joyhub_ProductPaymentRecommend.py)

2. 新增业务关键字层:
   - NewsCateManage.py
   - NewsManage.py
   - ProductCateManage.py
   - ProductAttrManage.py
   - ProductManage.py
   - FaqCateManage.py
   - FaqManage.py
   - BlogCateManage.py
   - AddressCountryManage.py
   - DownloadQrcodeManage.py
   - ProductPaymentRecommendManage.py

3. 接口层增强:
   - Dlizhan_interface.py 添加JoyHub相关接口封装

4. 功能增强:
   - run_tests.py 添加自动清除旧测试结果和报告功能
   - Joyhub_Product.py 添加数据库连接获取运费模板ID和品牌ID

5. 修复:
   - 修复产品创建测试用例缺少前置数据问题
2026-05-08 18:09:48 +08:00

287 lines
9.9 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 -*-
"""
统一测试执行文件
支持批量运行所有用例、按标签运行、按目录运行等
"""
import argparse
import os
import subprocess
import sys
import shutil
# 添加项目根目录到 Python 路径
current_file_path = os.path.abspath(__file__)
project_root = os.path.abspath(os.path.join(os.path.dirname(current_file_path), '../../'))
if project_root not in sys.path:
sys.path.insert(0, project_root)
print(project_root)
# 测试目录 - 已经包含了接口目录
TEST_CASE_DIR = 'dulizhan/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_PATH = os.path.join(project_root, 'allure', 'allure-2.28.0', 'bin', 'allure.bat')
print(ALLURE_REPORT_DIR)
def clean_old_results():
"""清除旧的测试结果和报告"""
print("清除旧的测试结果和报告...")
# 清除 allure-results 目录
if os.path.exists(ALLURE_RESULTS_DIR):
try:
shutil.rmtree(ALLURE_RESULTS_DIR)
print(f"已清除目录: {ALLURE_RESULTS_DIR}")
except Exception as e:
print(f"清除 {ALLURE_RESULTS_DIR} 失败: {e}")
# 清除 allure-report 目录
if os.path.exists(ALLURE_REPORT_DIR):
try:
shutil.rmtree(ALLURE_REPORT_DIR)
print(f"已清除目录: {ALLURE_REPORT_DIR}")
except Exception as e:
print(f"清除 {ALLURE_REPORT_DIR} 失败: {e}")
# 清除 .pytest_cache 目录
pytest_cache_dir = os.path.join(project_root, '.pytest_cache')
if os.path.exists(pytest_cache_dir):
try:
shutil.rmtree(pytest_cache_dir)
print(f"已清除目录: {pytest_cache_dir}")
except Exception as e:
print(f"清除 {pytest_cache_dir} 失败: {e}")
def ensure_dirs():
"""确保报告目录存在"""
os.makedirs(ALLURE_RESULTS_DIR, exist_ok=True)
os.makedirs(ALLURE_REPORT_DIR, exist_ok=True)
def run_pytest(args_list):
"""执行pytest命令"""
# 设置PYTHONPATH环境变量
env = os.environ.copy()
env['PYTHONPATH'] = project_root + (os.pathsep + env['PYTHONPATH'] if 'PYTHONPATH' in env else '')
# 检测Jenkins环境
is_jenkins = 'JENKINS_URL' in env or 'BUILD_NUMBER' in env
if is_jenkins and 'WORKSPACE' in env:
# Jenkins环境下使用绝对路径 - 确保与Jenkins Allure插件配置一致
global ALLURE_RESULTS_DIR, ALLURE_REPORT_DIR
# Jenkins配置的路径是 /home/dulizhan/allure-report
ALLURE_RESULTS_DIR = os.path.join(env['WORKSPACE'], 'allure-results')
ALLURE_REPORT_DIR = os.path.join(env['WORKSPACE'], 'allure-report')
ensure_dirs()
print(f"Jenkins环境检测到使用路径: {ALLURE_RESULTS_DIR}")
cmd = ['python', '-m', 'pytest'] + args_list
print("执行命令: {}".format(' '.join(cmd)))
# 执行命令
result = subprocess.run(' '.join(cmd), shell=True, cwd=project_root, env=env)
return result.returncode
def find_test_files(directory):
"""递归查找所有测试文件"""
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))
return test_files
def run_tests(target, test_type='all', **kwargs):
"""运行测试"""
base_args = ['-v', '--tb=short', '--alluredir={}'.format(ALLURE_RESULTS_DIR)]
if test_type == 'all':
print("运行所有测试用例...")
test_files = find_test_files(case_dir)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + base_args
elif test_type == 'feature':
print("按feature标签运行: {}".format(target))
test_files = find_test_files(case_dir)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + ['--allure-features={}'.format(target)] + base_args
elif test_type == 'story':
print("按story标签运行: {}".format(target))
test_files = find_test_files(case_dir)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + ['--allure-stories={}'.format(target)] + base_args
elif test_type == 'marker':
print("按pytest标记运行: {}".format(target))
test_files = find_test_files(case_dir)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + ['-m={}'.format(target)] + base_args
elif test_type == 'dir':
full_path = os.path.join(case_dir, target.replace('/', os.sep).replace('\\', os.sep))
if not os.path.exists(full_path):
print("错误: 目录不存在: {}".format(full_path))
return 1
print("按目录运行: {}".format(target))
test_files = find_test_files(full_path)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + base_args
elif test_type == 'file':
full_path = os.path.join(case_dir, target.replace('/', os.sep).replace('\\', os.sep))
if not os.path.exists(full_path):
print("错误: 文件不存在: {}".format(full_path))
return 1
print("按文件运行: {}".format(target))
args = [full_path] + base_args
elif test_type == 'keyword':
print("按关键字运行: {}".format(target))
test_files = find_test_files(case_dir)
if not test_files:
print("错误: 未找到测试文件")
return 1
args = test_files + ['-k={}'.format(target)] + base_args
else:
print("错误: 未知的测试类型: {}".format(test_type))
return 1
return run_pytest(args)
def generate_allure_report():
"""生成allure报告"""
print("生成Allure报告...")
if not os.path.exists(ALLURE_PATH):
print("警告: 未找到allure命令请检查路径: {}".format(ALLURE_PATH))
print("尝试使用系统环境变量中的allure...")
cmd = 'allure generate {} -o {} --clean'.format(ALLURE_RESULTS_DIR, ALLURE_REPORT_DIR)
else:
cmd = '"{}" generate {} -o {} --clean'.format(ALLURE_PATH, ALLURE_RESULTS_DIR, ALLURE_REPORT_DIR)
print("执行命令: {}".format(cmd))
try:
subprocess.run(cmd, shell=True, check=True)
print("Allure报告生成成功: {}".format(ALLURE_REPORT_DIR))
print("打开报告命令: allure open {}".format(ALLURE_REPORT_DIR))
return 0
except subprocess.CalledProcessError as e:
print("生成Allure报告失败: {}".format(e))
return 1
except FileNotFoundError:
print("警告: 未找到allure命令请先安装allure")
return 1
def open_allure_report():
"""打开allure报告"""
print("打开Allure报告...")
if not os.path.exists(ALLURE_PATH):
cmd = 'allure open {}'.format(ALLURE_REPORT_DIR)
else:
cmd = '"{}" open {}'.format(ALLURE_PATH, ALLURE_REPORT_DIR)
try:
subprocess.Popen(cmd, shell=True)
print("Allure报告已在浏览器中打开")
except FileNotFoundError:
print("警告: 未找到allure命令请先安装allure")
def main():
parser = argparse.ArgumentParser(
description='统一测试执行工具',
epilog="""
使用示例:
# 运行所有测试
python run_tests.py
# 按feature标签运行
python run_tests.py --feature "深圳采购工作台采购订单页面"
# 按目录运行相对于TestCase目录
python run_tests.py --dir "接口/JoyHub"
# 生成并打开报告
python run_tests.py --report --open
"""
)
# 运行选项
run_group = parser.add_mutually_exclusive_group(required=False)
run_group.add_argument('--feature', type=str, help='按allure feature标签运行')
run_group.add_argument('--story', type=str, help='按allure story标签运行')
run_group.add_argument('--dir', type=str, help='按目录运行相对于TestCase目录')
run_group.add_argument('--file', type=str, help='按文件运行相对于TestCase目录')
run_group.add_argument('--keyword', type=str, help='按关键字运行')
run_group.add_argument('--marker', type=str, help='按pytest标记运行')
# 报告选项
parser.add_argument('--report', action='store_true', help='生成Allure报告')
parser.add_argument('--open', action='store_true', help='打开Allure报告')
parser.add_argument('--no-report', action='store_true', help='不生成Allure报告')
args = parser.parse_args()
# 清除旧的测试结果和报告
clean_old_results()
# 确保目录存在
ensure_dirs()
# 执行测试
exit_code = 0
if args.feature:
exit_code = run_tests(args.feature, 'feature')
elif args.story:
exit_code = run_tests(args.story, 'story')
elif args.dir:
exit_code = run_tests(args.dir, 'dir')
elif args.file:
exit_code = run_tests(args.file, 'file')
elif args.keyword:
exit_code = run_tests(args.keyword, 'keyword')
elif args.marker:
exit_code = run_tests(args.marker, 'marker')
else:
# 默认运行所有测试
exit_code = run_tests(None, 'all')
# 生成报告
if args.report or (not args.no_report and exit_code == 0):
generate_allure_report()
if args.open:
open_allure_report()
print("=" * 80)
if exit_code == 0:
print("测试执行完成")
else:
print("测试执行失败,退出码: {}".format(exit_code))
print("=" * 80)
sys.exit(exit_code)
if __name__ == '__main__':
main()