我正在使用 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
src
中名称中带有“test”的模块我将 tox.ini 更改为
[tox]
envlist = py39
[testenv]
deps =
pytest
pytest-cov
-rrequirements.txt
-rrequirements.test
commands = pytest --cov=src {posargs:tests}
[pytest]
pythonpath = ./src
解决了这个问题。我猜 pytest 需要显式传入路径。