kards-env/tests/run_tests.py

107 lines
2.7 KiB
Python
Raw Normal View History

2025-09-05 17:05:43 +08:00
#!/usr/bin/env python3
"""
KARDS Battle System 测试运行器
运行所有测试并生成报告
"""
import sys
import os
import subprocess
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
def run_test_file(test_file):
"""运行单个测试文件"""
print(f"\n{'='*60}")
print(f"运行测试: {test_file.name}")
print('='*60)
try:
result = subprocess.run(
[sys.executable, str(test_file)],
capture_output=True,
text=True,
cwd=project_root
)
if result.returncode == 0:
print("✅ 测试通过")
if result.stdout:
print(result.stdout)
else:
print("❌ 测试失败")
if result.stdout:
print("STDOUT:", result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
return result.returncode == 0
except Exception as e:
print(f"❌ 运行测试时发生异常: {e}")
return False
def main():
"""运行所有测试"""
tests_dir = Path(__file__).parent
print("🧪 KARDS Battle System 测试套件")
print("="*60)
# 收集所有测试文件
test_files = []
# 单元测试
unit_tests = list((tests_dir / "unit").glob("test_*.py"))
test_files.extend(("单元测试", f) for f in unit_tests)
# 集成测试
integration_tests = list((tests_dir / "integration").glob("test_*.py"))
test_files.extend(("集成测试", f) for f in integration_tests)
# 示例测试
example_tests = list((tests_dir / "examples").glob("test_*.py"))
test_files.extend(("示例测试", f) for f in example_tests)
# 其他测试文件 (根目录下的测试)
other_tests = list(tests_dir.glob("test_*.py"))
test_files.extend(("其他测试", f) for f in other_tests)
if not test_files:
print("❌ 未找到测试文件")
return 1
# 运行测试
passed = 0
failed = 0
for test_type, test_file in test_files:
print(f"\n📁 {test_type}")
if run_test_file(test_file):
passed += 1
else:
failed += 1
# 总结报告
print(f"\n{'='*60}")
print("📊 测试结果汇总")
print('='*60)
print(f"✅ 通过: {passed}")
print(f"❌ 失败: {failed}")
print(f"📊 总计: {passed + failed}")
if failed == 0:
print("\n🎉 所有测试都通过了!")
return 0
else:
print(f"\n💥 有 {failed} 个测试失败")
return 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)