run_all_tests.py 2.3 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
运行所有API测试脚本
Python 3.7.8 兼容
"""

import os
import sys
import subprocess
from typing import List, Tuple


def run_script(script_path: str, args: List[str] = None) -> Tuple[bool, str]:
    """运行单个测试脚本"""
    cmd = [sys.executable, script_path]
    if args:
        cmd.extend(args)
    
    try:
        result = subprocess.run(
            cmd, 
            capture_output=True, 
            text=True, 
            cwd=os.path.dirname(__file__)
        )
        return result.returncode == 0, result.stdout + result.stderr
    except Exception as e:
        return False, str(e)


def main():
    """主函数:按顺序运行所有测试"""
    print("=" * 60)
    print("Apple ERP API 测试套件")
    print("=" * 60)
    
    # 测试脚本列表(按执行顺序)
    tests = [
        ("认证登录测试", "test_auth.py", ["admin", "password"]),
        ("用户管理测试", "test_users.py", []),
        ("角色管理测试", "test_roles.py", []),
        ("菜单管理测试", "test_menus.py", []),
        ("字典管理测试", "test_dicts.py", []),
    ]
    
    success_count = 0
    total_count = len(tests)
    
    for test_name, script_name, args in tests:
        print(f"\n{'='*20} {test_name} {'='*20}")
        script_path = os.path.join(os.path.dirname(__file__), script_name)
        
        if not os.path.exists(script_path):
            print(f"❌ 脚本不存在: {script_path}")
            continue
            
        success, output = run_script(script_path, args)
        
        if success:
            print(f"✅ {test_name} 执行成功")
            success_count += 1
        else:
            print(f"❌ {test_name} 执行失败")
        
        # 显示输出(截取前500字符避免过长)
        if output:
            print("输出:")
            print(output[:500] + ("..." if len(output) > 500 else ""))
    
    # 总结
    print("\n" + "=" * 60)
    print(f"测试完成: {success_count}/{total_count} 个测试通过")
    print("=" * 60)
    
    if success_count == total_count:
        print("🎉 所有测试都通过了!")
        return 0
    else:
        print("⚠️  部分测试失败,请检查输出信息")
        return 1


if __name__ == "__main__":
    sys.exit(main())