run_tests.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 语义逻辑审查模块 — 测试运行脚本
  5. 提供便捷的测试运行与服务器启动命令
  6. """
  7. import sys
  8. import os
  9. import subprocess
  10. def run_command(cmd, description):
  11. """运行命令并显示结果"""
  12. print(f"\n{'=' * 60}")
  13. print(f" {description}")
  14. print(f"{'=' * 60}\n")
  15. result = subprocess.run(cmd, shell=True)
  16. print(f"\n{'=' * 60}")
  17. if result.returncode == 0:
  18. print(f" 成功: {description}")
  19. else:
  20. print(f" 失败: {description} (exit code {result.returncode})")
  21. print(f"{'=' * 60}\n")
  22. return result.returncode
  23. def main():
  24. project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  25. os.chdir(project_root)
  26. print(f"项目根目录: {os.getcwd()}")
  27. print("\n" + "=" * 60)
  28. print(" 语义逻辑审查模块测试套件")
  29. print("=" * 60)
  30. print("\n请选择要执行的操作:")
  31. print(" 1. 运行 pytest 单元测试")
  32. print(" 2. 启动前端测试服务器 (Web UI)")
  33. print(" 3. 运行单元测试 + 启动服务器")
  34. print(" 4. 运行单元测试并生成覆盖率报告")
  35. print(" 5. 只运行失败的测试")
  36. print(" 0. 退出")
  37. choice = input("\n请输入选项 (0-5): ").strip()
  38. test_file = "utils_test/Semantic_Logic_Test/test_semantic_logic.py"
  39. server_file = "utils_test/Semantic_Logic_Test/semantic_logic_server.py"
  40. if choice == "1":
  41. env = os.environ.copy()
  42. env["PYTHONPATH"] = project_root
  43. return run_command(
  44. f'python -m pytest {test_file} -v --tb=short',
  45. "运行单元测试"
  46. )
  47. elif choice == "2":
  48. print("\n启动前端测试服务器...")
  49. print("启动后请访问: http://localhost:8766")
  50. return subprocess.run([sys.executable, server_file, "--port", "8766"]).returncode
  51. elif choice == "3":
  52. env = os.environ.copy()
  53. env["PYTHONPATH"] = project_root
  54. ret = run_command(
  55. f'python -m pytest {test_file} -v --tb=short',
  56. "运行单元测试"
  57. )
  58. if ret == 0:
  59. print("\n测试通过,启动前端服务器...")
  60. return subprocess.run([sys.executable, server_file, "--port", "8766"]).returncode
  61. return ret
  62. elif choice == "4":
  63. env = os.environ.copy()
  64. env["PYTHONPATH"] = project_root
  65. return run_command(
  66. f'python -m pytest {test_file} --cov=core.construction_review.component.reviewers.semantic_logic --cov-report=html --cov-report=term -v',
  67. "运行测试并生成覆盖率报告"
  68. )
  69. elif choice == "5":
  70. env = os.environ.copy()
  71. env["PYTHONPATH"] = project_root
  72. return run_command(
  73. f'python -m pytest {test_file} --lf -v',
  74. "只运行失败的测试"
  75. )
  76. elif choice == "0":
  77. print("\n再见!")
  78. return 0
  79. else:
  80. print("\n无效选项")
  81. return 1
  82. if __name__ == "__main__":
  83. sys.exit(main())