python config_error ai_generated true

CoverageWarning:模块 myapp 从未被导入。(module-not-imported) CoverageWarning:未收集到数据。(no-data-collected)

CoverageWarning: Module myapp was never imported. (module-not-imported) CoverageWarning: No data was collected. (no-data-collected)

ID: python/pytest-coverage-source-missing

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-09-19首次发现

版本兼容性

版本状态引入弃用备注
5.x active — — —
6.x active — — —
7.x active — — —

根因分析

pytest-cov 指向的源路径与可导入的包名不匹配,或者测试从未导入该包(例如仅测试导入了不同顶层名称的子模块)。

English

pytest-cov was pointed at a source path that doesn't match the importable package name, or tests never import the package (e.g., only test submodules that import a different top-level name).

generic

解决方案

  1. 95% 成功率
    pytest --cov=myapp --cov-report=term-missing tests/
  2. 92% 成功率
    # pyproject.toml
    [tool.coverage.run]
    source = ["myapp"]
    branch = true
  3. 85% 成功率
    # tests/conftest.py
    import myapp  # noqa: F401 ensures coverage sees the package

无效尝试

常见但无效的做法:

  1. 90% 失败

    Branch coverage doesn't fix missing source mapping.

  2. 90% 失败

    Threshold doesn't affect data collection.

  3. 85% 失败

    Extra deps don't fix path/name mismatch.