import os import subprocess import sys from Utils.enhance_report import enhance_report # 添加项目根目录到 Python 路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) def run_tests(): """运行测试并生成美化报告""" # 创建报告目录 os.makedirs('reports', exist_ok=True) os.makedirs('reports/assets/screenshots', exist_ok=True) os.makedirs('reports/assets/logs', exist_ok=True) print("开始运行测试...") # 运行测试 - 使用UTF-8编码避免中文乱码 try: result = subprocess.run([ 'pytest', 'Tests/', '--html=reports/report.html', '--self-contained-html', '-v' ], capture_output=True, text=True, encoding='utf-8', errors='replace') except Exception as e: print(f"运行测试时出错: {e}") # 尝试不使用编码参数 result = subprocess.run([ 'pytest', 'Tests/', '--html=reports/report.html', '--self-contained-html', '-v' ], capture_output=True, text=True) # 打印测试结果 print("\n测试输出:") if result.stdout: print(result.stdout) if result.stderr: print("错误输出:") print(result.stderr) # 增强报告 print("\n生成美化报告...") if os.path.exists('reports/report.html'): enhanced_report = enhance_report() print(f"美化报告已生成: {enhanced_report}") else: print("警告: 未找到原始报告文件") # 尝试直接运行增强报告 enhanced_report = enhance_report() if enhanced_report: print(f"美化报告已生成: {enhanced_report}") # 返回退出码 return result.returncode if __name__ == '__main__': exit_code = run_tests() print(f"\n测试执行完成,退出码: {exit_code}") sys.exit(exit_code)