Pytest cov 未读取 pyproject.toml

Pytest cov does not read pyproject.toml

Pytest cov 没有从 pyproject.toml 文件中读取它的设置。我正在使用 nox,所以我 运行 测试使用:

python3 -m nox

看来即使没有 nox 我也有同样的问题。

其实在运行宁了一个poetry install之后:

特别是,当测试失败时,我有以下输出(输出被剪切到最重要的内容):

WARNING: Failed to generate report: No data to report.

/Users/matteo/Library/Caches/pypoetry/virtualenvs/project-Nz69kfmJ-py3.7/lib/python3.7/site-packages/pytest_cov/plugin.py:271: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()


---------- coverage: platform darwin, python 3.7.7-final-0 -----------

FAIL Required test coverage of 100.0% not reached. Total coverage: 0.00%

Code with a reproducible error here。 要 运行 你需要 install poetry and to install nox.

将评论变成答案:

检查 src 目录的当前处理方式。现在,它似乎是一个名称空间包,这不是您想要的。切换到 src 布局:

# pyproject.toml

[tool.poetry]
...
packages = [
    { include = 'project', from = 'src' }
]

[tool.coverage.run]
...
source = ['project']

并修复 test_code.py 中的导入:

from src.project import code

from project import code

或删除 src 目录:

rootdir
├── project
│   └── __init__.py
└── tests
    └── test_code.py

并修复 test_code.py 中的导入。