Skip to content

Latest commit

 

History

History
163 lines (123 loc) · 3.68 KB

File metadata and controls

163 lines (123 loc) · 3.68 KB

测试框架优化 - 修改总结

日期: 2026-02-19 Issue: Issue 1 - 测试框架优化 状态: ✅ 已完成

修改内容

1. 删除 pytest.ini,统一使用 pyproject.toml

问题:

  • 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 项目最佳实践

2. 优化 pyproject.toml 中的 pytest 配置

新增选项:

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)
  • ✅ 更好的测试覆盖率报告
  • ✅ 失败测试自动重试
  • ✅ 测试超时保护

3. 增强 conftest.py 的测试隔离机制

3.1 改进 isolated_environment fixture

新增功能:

  • 强制垃圾回收(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

3.2 新增 parallel_safe fixture

功能:

  • 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

3.3 改进 numpy_rng fixture

改进:

  • 使用 parallel_safe 的种子
  • 支持 worker 感知的随机数生成
  • 确保并行测试的可重现性

测试验证

验证步骤

  1. 检查 WARNING 信息:

    pytest tests/ -v 2>&1 | grep -i warning

    预期:没有 WARNING

  2. 验证并行测试:

    pytest tests/ -n 2 -v

    预期:测试正常并行运行

  3. 检查测试覆盖率报告:

    pytest --cov=pymultiwfn

    预期:生成 terminal 和 HTML 覆盖率报告

  4. 验证测试隔离:

    pytest tests/ -n auto --reruns=2

    预期:测试间无干扰,重试正常工作


文件变更清单

文件 操作 说明
pytest.ini 删除 配置迁移到 pyproject.toml
pytest.ini.backup 创建 原始配置备份
pyproject.toml 修改 优化 pytest 配置
tests/conftest.py 修改 增强测试隔离和并行支持
TEST_FRAMEWORK_CHANGES.md 创建 修改总结文档

下一步计划

  1. ✅ 运行完整测试套件验证
  2. ✅ 检查测试覆盖率
  3. ✅ 验证并行测试功能
  4. ⏳ Git commit(等待验证通过)
  5. ⏳ 推送到远程仓库

作者: Ralph Loop Coder Agent 审核: Ralph Loop Verifier Agent(待审核)