#!/usr/bin/env python """测试运行脚本 - 方便运行各种类型的测试""" import sys import subprocess import argparse def run_command(cmd, description): """运行命令并打印结果""" print(f"\n{'='*60}") print(f"运行: {description}") print(f"命令: {' '.join(cmd)}") print('='*60) result = subprocess.run(cmd, shell=False) return result.returncode def main(): parser = argparse.ArgumentParser(description='运行 ProxyPool 测试') parser.add_argument( 'type', nargs='?', default='all', choices=['all', 'unit', 'integration', 'e2e', 'coverage'], help='测试类型 (默认: all)' ) parser.add_argument( '-v', '--verbose', action='store_true', help='详细输出' ) parser.add_argument( '-k', metavar='EXPRESSION', help='只运行匹配表达式的测试 (pytest -k 选项)' ) args = parser.parse_args() # 基础命令 base_cmd = [sys.executable, "-m", "pytest"] if args.verbose: base_cmd.append("-v") if args.k: base_cmd.extend(["-k", args.k]) exit_code = 0 if args.type == 'all': exit_code = run_command( base_cmd + ["tests/"], "所有测试" ) elif args.type == 'unit': exit_code = run_command( base_cmd + ["tests/unit/"], "单元测试" ) elif args.type == 'integration': exit_code = run_command( base_cmd + ["tests/integration/"], "集成测试" ) elif args.type == 'e2e': exit_code = run_command( base_cmd + ["tests/e2e/"], "端到端测试" ) elif args.type == 'coverage': exit_code = run_command( [sys.executable, "-m", "pytest", "tests/", "--cov=app", "--cov-report=html", "--cov-report=term"] + (["-v"] if args.verbose else []), "覆盖率测试" ) print(f"\n{'='*60}") if exit_code == 0: print("✅ 所有测试通过!") else: print(f"❌ 测试失败 (退出码: {exit_code})") print('='*60) return exit_code if __name__ == "__main__": sys.exit(main())