feat: Add coverage report generation and enhance testing framework
- Introduced a new `.coverage` file for tracking test coverage metrics. - Added `pytest.ini` configuration for streamlined testing setup, including coverage reporting and logging options. - Created `requirements-test.txt` to manage testing dependencies, ensuring a consistent testing environment. - Implemented a `run_tests.py` script to facilitate running tests with various options, including unit and integration tests. - Established a structured testing directory with unit and integration tests, enhancing code reliability and maintainability. - Updated documentation to reflect new testing procedures and configurations.
This commit is contained in:
1 parent
ce4a332dc0
commit
539e580147
44 files changed
+11824
-9501
No files matched your search
+107
@@ -0,0 +1,107 @@
|
||||
#!/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()
|
||||
|
||||
Reference in new issue
Block a user