python config_error ai_generated true

CoverageWarning: 未收集到数据。(no-data-collected) WARNING: 未能找到模块 'myapp' 的数据

CoverageWarning: No data was collected. (no-data-collected) WARNING: Failed to find data for module 'myapp'

ID: python/pytest-coverage-no-data-collected

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

版本兼容性

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

根因分析

coverage run 指向了错误的源码,或者 pytest 在与 coverage 插桩不同的解释器/子进程中执行。常见于使用 pytest-xdist 但未加 --cov,或测试启动的子进程未被 coverage 追踪。

English

coverage run was pointed at the wrong source, or pytest is executed under a different interpreter/subprocess than the one coverage instruments. Common when using pytest-xdist without --cov or when tests spawn subprocesses that coverage does not trace.

generic

解决方案

  1. 94% 成功率
    pip install pytest-cov
    pytest --cov=myapp --cov-report=term-missing -n auto
  2. 85% 成功率
    # .coveragerc
    [run]
    concurrency = multiprocessing
    parallel = True
    source = myapp

无效尝试

常见但无效的做法:

  1. 70% 失败

    Broadens the source but if coverage is not started under the same process, still collects nothing.

  2. 65% 失败

    Correct in principle, but if pytest-xdist is active the workers are separate processes and the main process records no data.