run_tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 快速运行测试脚本
  5. 提供便捷的测试运行命令
  6. """
  7. import sys
  8. import os
  9. import subprocess
  10. from pathlib import Path
  11. def run_command(cmd, description):
  12. """运行命令并显示结果"""
  13. print(f"\n{'='*60}")
  14. print(f"🚀 {description}")
  15. print(f"{'='*60}\n")
  16. result = subprocess.run(cmd, shell=True)
  17. if result.returncode == 0:
  18. print(f"\n✅ {description} - 成功")
  19. else:
  20. print(f"\n❌ {description} - 失败")
  21. return result.returncode
  22. def main():
  23. """主函数"""
  24. # 切换到项目根目录
  25. project_root = Path(__file__).parent.parent
  26. os.chdir(project_root)
  27. print(f"📁 工作目录: {os.getcwd()}")
  28. # 检查是否安装了测试依赖
  29. print("\n📦 检查测试依赖...")
  30. try:
  31. import pytest
  32. import pytest_asyncio
  33. print("✅ 测试依赖已安装")
  34. except ImportError:
  35. print("❌ 缺少测试依赖,正在安装...")
  36. subprocess.run([
  37. sys.executable, "-m", "pip", "install",
  38. "-r", "Semantic_Logic_Test/requirements_test.txt"
  39. ])
  40. # 显示菜单
  41. print("\n" + "="*60)
  42. print("🧪 语义逻辑审查模块测试套件")
  43. print("="*60)
  44. print("\n请选择要运行的测试:")
  45. print(" 1. 运行所有测试")
  46. print(" 2. 运行所有测试(详细输出)")
  47. print(" 3. 运行基础功能测试")
  48. print(" 4. 运行边界情况测试")
  49. print(" 5. 运行测试并生成覆盖率报告")
  50. print(" 6. 运行测试并生成 HTML 报告")
  51. print(" 7. 只运行失败的测试")
  52. print(" 8. 运行特定测试(手动输入)")
  53. print(" 0. 退出")
  54. choice = input("\n请输入选项 (0-8): ").strip()
  55. test_file = "Semantic_Logic_Test/test_semantic_logic.py"
  56. if choice == "1":
  57. return run_command(
  58. f"pytest {test_file} -v",
  59. "运行所有测试"
  60. )
  61. elif choice == "2":
  62. return run_command(
  63. f"pytest {test_file} -v -s",
  64. "运行所有测试(详细输出)"
  65. )
  66. elif choice == "3":
  67. return run_command(
  68. f"pytest {test_file}::TestSemanticLogicReviewer -v",
  69. "运行基础功能测试"
  70. )
  71. elif choice == "4":
  72. return run_command(
  73. f"pytest {test_file}::TestEdgeCases -v",
  74. "运行边界情况测试"
  75. )
  76. elif choice == "5":
  77. return run_command(
  78. f"pytest {test_file} --cov=core.construction_review.component.reviewers.semantic_logic --cov-report=html --cov-report=term",
  79. "运行测试并生成覆盖率报告"
  80. )
  81. elif choice == "6":
  82. return run_command(
  83. f"pytest {test_file} --html=Semantic_Logic_Test/report.html --self-contained-html",
  84. "运行测试并生成 HTML 报告"
  85. )
  86. elif choice == "7":
  87. return run_command(
  88. f"pytest {test_file} --lf -v",
  89. "只运行失败的测试"
  90. )
  91. elif choice == "8":
  92. test_name = input("请输入测试方法名称(例如: test_reviewer_initialization): ").strip()
  93. return run_command(
  94. f"pytest {test_file}::TestSemanticLogicReviewer::{test_name} -v -s",
  95. f"运行测试: {test_name}"
  96. )
  97. elif choice == "0":
  98. print("\n👋 再见!")
  99. return 0
  100. else:
  101. print("\n❌ 无效的选项")
  102. return 1
  103. if __name__ == "__main__":
  104. sys.exit(main())