使用 tox 运行测试时未收集数据,但使用 pytest 运行测试时未收集数据

问题描述 投票:0回答:1

我正在使用 tox 和 pytest 运行单元测试。当我使用

tox
运行测试时,它会运行测试并显示我的
src
目录的树,但打印
CoverageWarning: No data was collected. (no-data-collected)
。当我使用
pytest
运行时,我得到完全相同的输出,除了没有错误,并且覆盖率数字按预期打印。我的
tox.ini
看起来像这样:

[tox]
envlist = py39

[testenv]
deps = 
    pytest
    pytest-cov
    -rrequirements.txt
    -rrequirements.test

commands = pytest {posargs}

[pytest]
addopts =
    -v
    --cov=src
    tests/

我尝试过SO上提到的其他解决方案,例如

  • 确保
    src
    中的每个目录都有
    __init__.py
  • 确保测试目录有
    __init__.py
  • 删除
    src
  • 中每个Python文件的执行权限
  • 向 tox.ini 添加包含/省略语句
  • 删除
    src
    中名称中带有“test”的模块
python python-3.x pytest tox coverage.py
1个回答
0
投票

我将 tox.ini 更改为

[tox]
envlist = py39

[testenv]
deps = 
    pytest
    pytest-cov
    -rrequirements.txt
    -rrequirements.test

commands = pytest --cov=src {posargs:tests}

[pytest]
pythonpath = ./src

解决了这个问题。我猜 pytest 需要显式传入路径。

© www.soinside.com 2019 - 2024. All rights reserved.