python - 从 pip 转移到诗歌,现在 pytest-cov 不会收集覆盖率数据

标签 python pytest tox coverage.py python-poetry

我最近开始使用poetry来管理项目依赖项, 而不是使用 requirements.txttest-requirements.txt

自从进行更改后,我无法让覆盖率测试发挥作用 正确。在这两种情况下,我都使用 tox 来驱动测试(并且我 安装了 tox-poetry 扩展)。

我的 tox.ini 目前看起来像这样:

[tox]
isolated_build = True
envlist = pep8,unit

[testenv]
whitelist_externals = poetry

[testenv:venv]
commands = {posargs}

[testenv:pep8]
commands =
    poetry run flake8 {posargs:symtool}

[testenv:unit]
commands =
    poetry run pytest --cov=symtool {posargs} tests/unit

以前,它看起来像这样:

[tox]
envlist = pep8,unit

[testenv]
usedevelop = True
install_command = pip install -U {opts} {packages}
deps = -r{toxinidir}/requirements.txt
    -r{toxinidir}/test-requirements.txt

[testenv:venv]
commands = {posargs}

[testenv:pep8]
commands =
    flake8 {posargs:symtool}

[testenv:unit]
commands =
    pytest --cov=symtool {posargs} tests/unit

自从对poetry进行更改后,当我运行例如tox -e 单元,我明白了:

unit run-test: commands[0] | poetry run pytest --cov=symtool tests/unit
===================================== test session starts =====================================
platform linux -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .tox/unit/.pytest_cache
rootdir: /home/lars/projects/symtool, configfile: tox.ini
plugins: cov-2.11.1
collected 14 items

tests/unit/test_disasm.py .....                                                         [ 35%]
tests/unit/test_symtool.py .........                                                    [100%]r
Coverage.py warning: No data was collected. (no-data-collected)

我正在尝试解决no-data-collected问题。根据 pytest --help--cov 参数设置路径包 名称:

 --cov=[SOURCE]        Path or package name to measure during execution

存储库的根目录(上面输出中的 rootdir 来自tox)看起来像这样:

asm
pyproject.toml
README.md
reference
symtool
tests
tox.ini

那里肯定有一个包含该包的 symtool 目录。 即使测试没有在项目根目录中运行 目录下,测试环境中安装了symtool包, 单元测试实际上已经通过(所有 其中包括 import symtool 的一些变体)。

如何让保险再次发挥作用?

最佳答案

将我的评论转化为答案:

这是 pytest-covtox 的老问题(首次在 issue #38 中报告)。 tox 将您的项目安装为第三方包,pytest 将从站点导入它(例如,从 .tox/unit/lib/python3.X/site -packages(如果作业名为 unit),而 --cov=symtool 指示 pytest-cov 收集覆盖范围项目根目录中的 symtool 目录。

一种解决方案是切换到 src 布局:

├── pyproject.toml
├── README.md
├── reference
├── src
|   └── symtool
├── tests
└── tox.ini

在您的pyproject.toml中,您需要将poetry指向源目录:

[tool.poetry]
...
packages = [
    { include = "symtool", from = "src" },
]

现在,src 将阻止从源代码树导入代码,因此 --cov=symtool 将收集已安装模块的覆盖范围,这是唯一的选择。对于开发过程的其余部分,您不应该遇到麻烦,因为 poetry install 以可编辑模式安装项目,因此其余部分应该可以正常工作。

另一个选项是使用 tox 跳过软件包安装并以可编辑模式安装。放置在 tox.ini 中的示例片段:

[testenv]
whitelist_externals =
    poetry
skip_install = true
commands_pre =
    poetry install
commands =
    poetry run pytest ...

这几乎杀死了对已安装模块的测试(你不再明确测试你的项目是否可以安装在空白的 venv 中,而是使用源代码树中的内容),所以我会使用 src 布局选项。

关于python - 从 pip 转移到诗歌,现在 pytest-cov 不会收集覆盖率数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66090538/

相关文章:

python - 如何根据范围过滤数字?

python - 无法理解如何从wxpython中的checklistbox获取数据

python - 为什么 Python 告诉我在随机抽样之前先排序?

python - 流量控制和失败 : Database access not allowed, 使用 "django_db".... 错误

python - 使用 cython 运行 pytest - 如何在 pytest 中编译 cython 模块?

python - Pytest 装置范围

python - 运行后如何清理有毒环境?

python - Python 3 Miniconda 环境上的 wxPython

python - 如何防止 tox 生成一堆 .egg 目录?

python - 带有 tox 的 Travis-ci 在 python2.6、python3.3 和 pypy 上失败