日期: 2026-02-19 Issue: Issue 1 - 测试框架优化 状态: ✅ 已完成
问题:
- pytest.ini 和 pyproject.toml 都包含 pytest 配置
- 导致 WARNING: ignoring pytest config in pyproject.toml!
解决方案:
- 备份 pytest.ini 为 pytest.ini.backup
- 删除 pytest.ini
- 使用 pyproject.toml 中的
[tool.pytest.ini_options]配置
影响:
- ✅ 消除了 WARNING 信息
- ✅ 统一了配置管理
- ✅ 符合现代 Python 项目最佳实践
新增选项:
addopts = [
"--strict-config", # 严格配置验证
"--tb=short", # 短格式 traceback
"--cov-report=term-missing:skip-covered", # 终端显示覆盖率
"--cov-report=html:htmlcov", # HTML 覆盖率报告
"--reruns=2", # 失败测试重试 2 次
"--reruns-delay=1", # 重试延迟 1 秒
"--timeout=600", # 测试超时 600 秒
]新增使用说明:
# 并行测试(自动检测 CPU 核心数)
pytest --cov=pymultiwfn -n auto
# 指定 4 个 worker 并行测试
pytest --no-cov -n 4
# 跳过慢速测试,并行运行
pytest -m "not slow" -n auto
# 只运行单元测试
pytest -m "unit" -n auto影响:
- ✅ 支持并行测试(pytest-xdist)
- ✅ 更好的测试覆盖率报告
- ✅ 失败测试自动重试
- ✅ 测试超时保护
新增功能:
- 强制垃圾回收(gc.collect())
- 清理模块属性,重置全局状态
- 更安全的模块重新加载机制
代码改进:
# Before:
yield # Run the test
# After:
gc.collect() # Force GC before test
yield # Run the test
gc.collect() # Force GC after test
# Clear module attributes and reload功能:
- Worker 识别(worker_id)
- 每个 worker 独立的临时目录
- 每个 worker 独立的随机种子
用途:
def test_parallel_safe(parallel_safe):
worker_id = parallel_safe['worker_id'] # "gw0", "gw1", etc.
temp_dir = parallel_safe['temp_dir'] # Unique temp dir
seed = parallel_safe['seed'] # Unique random seed改进:
- 使用
parallel_safe的种子 - 支持 worker 感知的随机数生成
- 确保并行测试的可重现性
-
检查 WARNING 信息:
pytest tests/ -v 2>&1 | grep -i warning
预期:没有 WARNING
-
验证并行测试:
pytest tests/ -n 2 -v
预期:测试正常并行运行
-
检查测试覆盖率报告:
pytest --cov=pymultiwfn
预期:生成 terminal 和 HTML 覆盖率报告
-
验证测试隔离:
pytest tests/ -n auto --reruns=2
预期:测试间无干扰,重试正常工作
| 文件 | 操作 | 说明 |
|---|---|---|
| pytest.ini | 删除 | 配置迁移到 pyproject.toml |
| pytest.ini.backup | 创建 | 原始配置备份 |
| pyproject.toml | 修改 | 优化 pytest 配置 |
| tests/conftest.py | 修改 | 增强测试隔离和并行支持 |
| TEST_FRAMEWORK_CHANGES.md | 创建 | 修改总结文档 |
- ✅ 运行完整测试套件验证
- ✅ 检查测试覆盖率
- ✅ 验证并行测试功能
- ⏳ Git commit(等待验证通过)
- ⏳ 推送到远程仓库
作者: Ralph Loop Coder Agent 审核: Ralph Loop Verifier Agent(待审核)