#!/usr/bin/env python # -*- coding: utf-8 -*- """ 语义逻辑审查模块 — 测试运行脚本 提供便捷的测试运行与服务器启动命令 """ import sys import os import subprocess def run_command(cmd, description): """运行命令并显示结果""" print(f"\n{'=' * 60}") print(f" {description}") print(f"{'=' * 60}\n") result = subprocess.run(cmd, shell=True) print(f"\n{'=' * 60}") if result.returncode == 0: print(f" 成功: {description}") else: print(f" 失败: {description} (exit code {result.returncode})") print(f"{'=' * 60}\n") return result.returncode def main(): project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) os.chdir(project_root) print(f"项目根目录: {os.getcwd()}") print("\n" + "=" * 60) print(" 语义逻辑审查模块测试套件") print("=" * 60) print("\n请选择要执行的操作:") print(" 1. 运行 pytest 单元测试") print(" 2. 启动前端测试服务器 (Web UI)") print(" 3. 运行单元测试 + 启动服务器") print(" 4. 运行单元测试并生成覆盖率报告") print(" 5. 只运行失败的测试") print(" 0. 退出") choice = input("\n请输入选项 (0-5): ").strip() test_file = "utils_test/Semantic_Logic_Test/test_semantic_logic.py" server_file = "utils_test/Semantic_Logic_Test/semantic_logic_server.py" if choice == "1": env = os.environ.copy() env["PYTHONPATH"] = project_root return run_command( f'python -m pytest {test_file} -v --tb=short', "运行单元测试" ) elif choice == "2": print("\n启动前端测试服务器...") print("启动后请访问: http://localhost:8766") return subprocess.run([sys.executable, server_file, "--port", "8766"]).returncode elif choice == "3": env = os.environ.copy() env["PYTHONPATH"] = project_root ret = run_command( f'python -m pytest {test_file} -v --tb=short', "运行单元测试" ) if ret == 0: print("\n测试通过,启动前端服务器...") return subprocess.run([sys.executable, server_file, "--port", "8766"]).returncode return ret elif choice == "4": env = os.environ.copy() env["PYTHONPATH"] = project_root return run_command( f'python -m pytest {test_file} --cov=core.construction_review.component.reviewers.semantic_logic --cov-report=html --cov-report=term -v', "运行测试并生成覆盖率报告" ) elif choice == "5": env = os.environ.copy() env["PYTHONPATH"] = project_root return run_command( f'python -m pytest {test_file} --lf -v', "只运行失败的测试" ) elif choice == "0": print("\n再见!") return 0 else: print("\n无效选项") return 1 if __name__ == "__main__": sys.exit(main())