mi-task/test_integration.py

219 lines
6.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
集成测试脚本
用于验证single-test模块与main.py的集成是否正常工作
"""
import sys
import os
import importlib.util
def test_imports():
"""测试模块导入"""
print("🔍 测试模块导入...")
# 测试配置文件导入
try:
from config import get_config, SYSTEM_CONFIG
print("✅ config.py 导入成功")
except ImportError as e:
print(f"❌ config.py 导入失败: {e}")
return False
# 测试主程序导入
try:
from main import main, legacy_main, run_complete_task_mode, run_test_mode, run_interactive_test_mode
print("✅ main.py 导入成功")
except ImportError as e:
print(f"❌ main.py 导入失败: {e}")
return False
# 测试启动脚本导入
try:
from start import main_cli
print("✅ start.py 导入成功")
except ImportError as e:
print(f"❌ start.py 导入失败: {e}")
return False
# 测试single-test模块导入
try:
from single_test.test_main import main_menu, run_interactive_test_selection
print("✅ single-test模块导入成功")
except ImportError as e:
print(f"⚠️ single-test模块导入失败: {e}")
print(" 这是正常的如果single-test目录不存在的话")
return True
def test_config():
"""测试配置功能"""
print("\n🔧 测试配置功能...")
try:
from config import get_config, update_config, print_config
# 测试获取配置
time_sleep = get_config('SYSTEM', 'TIME_SLEEP', 5000)
print(f"✅ 获取配置成功: TIME_SLEEP = {time_sleep}")
# 测试更新配置
update_config('SYSTEM', 'TIME_SLEEP', 3000)
new_time_sleep = get_config('SYSTEM', 'TIME_SLEEP', 5000)
print(f"✅ 更新配置成功: TIME_SLEEP = {new_time_sleep}")
# 恢复原始配置
update_config('SYSTEM', 'TIME_SLEEP', time_sleep)
return True
except Exception as e:
print(f"❌ 配置功能测试失败: {e}")
return False
def test_file_structure():
"""测试文件结构"""
print("\n📁 测试文件结构...")
required_files = [
'main.py',
'start.py',
'config.py',
'README_INTEGRATION.md',
]
optional_files = [
'single-test/__init__.py',
'single-test/test_main.py',
'single-test/README.md',
]
all_good = True
for file_path in required_files:
if os.path.exists(file_path):
print(f"{file_path} 存在")
else:
print(f"{file_path} 不存在")
all_good = False
for file_path in optional_files:
if os.path.exists(file_path):
print(f"{file_path} 存在")
else:
print(f"⚠️ {file_path} 不存在(可选)")
return all_good
def test_functionality():
"""测试功能性"""
print("\n⚙️ 测试功能性...")
try:
# 测试配置获取
from config import get_config
single_test_enabled = get_config('SYSTEM', 'ENABLE_SINGLE_TEST', True)
print(f"✅ 单独测试功能启用状态: {single_test_enabled}")
# 测试模块可用性检查
from main import SINGLE_TEST_AVAILABLE
print(f"✅ single-test模块可用性: {SINGLE_TEST_AVAILABLE}")
# 测试函数存在性
from main import print_main_menu
print("✅ print_main_menu 函数存在")
return True
except Exception as e:
print(f"❌ 功能性测试失败: {e}")
return False
def print_integration_status():
"""打印集成状态"""
print("\n" + "="*60)
print(" 集成状态报告")
print("="*60)
# 检查各个组件
components = {
"主程序 (main.py)": os.path.exists('main.py'),
"启动脚本 (start.py)": os.path.exists('start.py'),
"配置文件 (config.py)": os.path.exists('config.py'),
"集成文档": os.path.exists('README_INTEGRATION.md'),
"测试模块目录": os.path.exists('single-test'),
"测试模块初始化": os.path.exists('single-test/__init__.py'),
"测试主程序": os.path.exists('single-test/test_main.py'),
}
for component, exists in components.items():
status = "✅ 可用" if exists else "❌ 缺失"
print(f" {component:<20} {status}")
print("\n📊 集成完成度:")
available_count = sum(1 for exists in components.values() if exists)
total_count = len(components)
completion_rate = (available_count / total_count) * 100
print(f" {available_count}/{total_count} 组件可用 ({completion_rate:.1f}%)")
if completion_rate >= 80:
print(" 🎉 集成状态: 良好")
elif completion_rate >= 60:
print(" ⚠️ 集成状态: 部分可用")
else:
print(" ❌ 集成状态: 需要修复")
print("="*60)
def main():
"""主测试函数"""
print("🚀 开始集成测试...")
print("="*60)
# 运行各项测试
tests = [
("模块导入", test_imports),
("配置功能", test_config),
("文件结构", test_file_structure),
("功能性", test_functionality),
]
results = []
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"{test_name}测试异常: {e}")
results.append((test_name, False))
# 打印测试结果
print("\n" + "="*60)
print(" 测试结果")
print("="*60)
passed = 0
for test_name, result in results:
status = "✅ 通过" if result else "❌ 失败"
print(f" {test_name:<15} {status}")
if result:
passed += 1
print(f"\n📈 测试统计: {passed}/{len(results)} 通过")
# 打印集成状态
print_integration_status()
# 提供使用建议
print("\n💡 使用建议:")
if passed == len(results):
print(" 🎉 所有测试通过!您可以开始使用集成系统了")
print(" 📖 运行 'python main.py' 开始使用")
print(" 📖 运行 'python start.py -h' 查看更多选项")
else:
print(" ⚠️ 部分测试失败,请检查:")
for test_name, result in results:
if not result:
print(f" - {test_name}")
print(" 📖 查看 README_INTEGRATION.md 获取帮助")
if __name__ == "__main__":
main()