- Removed excessive blank lines from various markdown and Python files to improve readability and maintainability. - Ensured consistent formatting across documentation and code files, contributing to overall project clarity. - Enhanced logging mechanisms for better consistency and debugging support.
183 lines
2.6 KiB
Python
183 lines
2.6 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
测试运行脚本
|
||
===========
|
||
|
||
用法:
|
||
python run_tests.py # 运行所有测试
|
||
python run_tests.py --unit # 只运行单元测试
|
||
python run_tests.py --integration # 只运行集成测试
|
||
python run_tests.py --coverage # 生成覆盖率报告
|
||
python run_tests.py --parallel # 并行运行测试
|
||
"""
|
||
|
||
import sys
|
||
import subprocess
|
||
import argparse
|
||
from pathlib import Path
|
||
|
||
|
||
def main():
|
||
parser = argparse.ArgumentParser(description="运行测试套件")
|
||
parser.add_argument(
|
||
"--unit",
|
||
action="store_true",
|
||
help="只运行单元测试"
|
||
)
|
||
parser.add_argument(
|
||
"--integration",
|
||
action="store_true",
|
||
help="只运行集成测试"
|
||
)
|
||
parser.add_argument(
|
||
"--coverage",
|
||
action="store_true",
|
||
help="生成覆盖率报告"
|
||
)
|
||
parser.add_argument(
|
||
"--parallel",
|
||
action="store_true",
|
||
help="并行运行测试(使用pytest-xdist)"
|
||
)
|
||
parser.add_argument(
|
||
"--verbose",
|
||
"-v",
|
||
action="store_true",
|
||
help="详细输出"
|
||
)
|
||
parser.add_argument(
|
||
"--markers",
|
||
"-m",
|
||
help="只运行指定标记的测试(例如: -m unit)"
|
||
)
|
||
parser.add_argument(
|
||
"--file",
|
||
"-f",
|
||
help="只运行指定文件的测试"
|
||
)
|
||
|
||
args = parser.parse_args()
|
||
|
||
# 构建pytest命令
|
||
cmd = ["python", "-m", "pytest"]
|
||
|
||
if args.unit:
|
||
cmd.extend(["tests/unit"])
|
||
elif args.integration:
|
||
cmd.extend(["tests/integration"])
|
||
elif args.file:
|
||
cmd.append(args.file)
|
||
else:
|
||
cmd.append("tests")
|
||
|
||
if args.markers:
|
||
cmd.extend(["-m", args.markers])
|
||
|
||
if args.parallel:
|
||
import multiprocessing
|
||
num_workers = multiprocessing.cpu_count()
|
||
cmd.extend(["-n", str(num_workers)])
|
||
|
||
if args.verbose:
|
||
cmd.append("-vv")
|
||
|
||
if args.coverage:
|
||
cmd.extend([
|
||
"--cov=.",
|
||
"--cov-report=html:htmlcov",
|
||
"--cov-report=term-missing"
|
||
])
|
||
|
||
# 运行测试
|
||
print(f"运行命令: {' '.join(cmd)}")
|
||
result = subprocess.run(cmd)
|
||
|
||
if result.returncode == 0:
|
||
print("\n✅ 所有测试通过!")
|
||
if args.coverage:
|
||
print("\n📊 覆盖率报告已生成: htmlcov/index.html")
|
||
else:
|
||
print("\n❌ 测试失败")
|
||
sys.exit(1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|