You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何收集pytest启动的Python进程覆盖率?遇无数据收集警告

问题:收集pytest启动的外部Python进程代码覆盖率异常

我尝试收集pytest启动的Python进程的代码覆盖率,执行了以下步骤:

  • 在本地site-packages目录创建sitecustomize.py模块,代码如下:
#/home/Olumide/.local/lib/python3.10/site-packages/sitecustomize.py
import coverage
coverage.process_startup()
  • 设置环境变量:export COVERAGE_PROCESS_START=True
  • 运行测试命令:coverage run --rcfile=.coveragerc -m pytest tests/gui/test_screenshots.py
    当前目录的.coveragerc配置内容:
[run]
source = src/
parallel = True
relative_files = True
omit =
    **/tests/*

注:测试脚本tests/gui/test_screenshots.py会启动我需要收集覆盖率的外部Python应用。

运行后仍收到警告:

/home/Olumide/repos/app/3.10_env/lib/python3.10/site-packages/coverage/control.py:887: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")

更新

在测试运行目录中找到一个53248字节的覆盖率文件(文件名:.coverage.Ubuntu-22.5371.XcjyqXNx),执行coverage html --data-file=.coverage.Ubuntu-22.5371.XcjyqXNx可以生成HTML报告。看起来已经获取到覆盖率,但奇怪的是只有__init__.py文件有100%的覆盖率统计,我疑惑这是否就是警告信息所指的情况。

内容的提问来源于stack exchange,提问作者Olumide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:15:07