testRun.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import os
  2. import subprocess
  3. import sys
  4. from Utils.enhance_report import enhance_report
  5. # 添加项目根目录到 Python 路径
  6. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  7. def run_tests():
  8. """运行测试并生成美化报告"""
  9. # 创建报告目录
  10. os.makedirs('reports', exist_ok=True)
  11. os.makedirs('reports/assets/screenshots', exist_ok=True)
  12. os.makedirs('reports/assets/logs', exist_ok=True)
  13. print("开始运行测试...")
  14. # 运行测试 - 使用UTF-8编码避免中文乱码
  15. try:
  16. result = subprocess.run([
  17. 'pytest',
  18. 'Tests/',
  19. '--html=reports/report.html',
  20. '--self-contained-html',
  21. '-v'
  22. ], capture_output=True, text=True, encoding='utf-8', errors='replace')
  23. except Exception as e:
  24. print(f"运行测试时出错: {e}")
  25. # 尝试不使用编码参数
  26. result = subprocess.run([
  27. 'pytest',
  28. 'Tests/',
  29. '--html=reports/report.html',
  30. '--self-contained-html',
  31. '-v'
  32. ], capture_output=True, text=True)
  33. # 打印测试结果
  34. print("\n测试输出:")
  35. if result.stdout:
  36. print(result.stdout)
  37. if result.stderr:
  38. print("错误输出:")
  39. print(result.stderr)
  40. # 增强报告
  41. print("\n生成美化报告...")
  42. if os.path.exists('reports/report.html'):
  43. enhanced_report = enhance_report()
  44. print(f"美化报告已生成: {enhanced_report}")
  45. else:
  46. print("警告: 未找到原始报告文件")
  47. # 尝试直接运行增强报告
  48. enhanced_report = enhance_report()
  49. if enhanced_report:
  50. print(f"美化报告已生成: {enhanced_report}")
  51. # 返回退出码
  52. return result.returncode
  53. if __name__ == '__main__':
  54. exit_code = run_tests()
  55. print(f"\n测试执行完成,退出码: {exit_code}")
  56. sys.exit(exit_code)