PytestWarning:无法生成报告:没有要报告的数据

PytestWarning: Failed to generate report: No data to report

我正在尝试通过 运行 以下命令获取有关我的代码的覆盖率报告:

python3 -m pytest --cov-config=.coveragerc --cov=main/lambda_function tests

我是 运行 回购中 pycharm 终端中的命令,我的主要代码位于 main/lambda_function.

我确实正确配置了 .coveragerc,但是一旦我将 lambda_function 文件重新定位到 main 下,我的覆盖率报告就停止工作了:

[run]
source =
    lambda_function
    rds_config
[paths]
source =
    src/lambda_function
[report]
show_missing = true
precision = 2

我尝试添加 main/ 但出现错误:

Coverage.py warning: Module main/lambda_function was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

我错过了什么?

--cov 的值应该是记录的路径:

--cov=PATH

Measure coverage for filesystem path. (multi-allowed)

尝试将覆盖路径更改为包含目录main

python3 -m pytest --cov-config=.coveragerc --cov=main tests

或者尝试使用 python 模块名称表示法,使用点 . 并且不带文件扩展名 main.lambda_function

python3 -m pytest --cov-config=.coveragerc --cov=main.lambda_function tests

我将 pytest 与 PyCharm 和 pyproject.toml 一起使用,而不是 .coveragerc

有了配置

[tool.pytest.ini_options]
# Also see
# https://docs.pytest.org/en/6.2.x/customize.html#pyproject-toml
# https://pytest-cov.readthedocs.io/en/latest/config.html
# If you want to see console output from tests, include -s flag
addopts = [
    '--cov=src',
    '--cov-fail-under=98',
    '--cov-report=html:pytest',
    '--cov-report=term-missing',
    '--junitxml=report.xml'
]

我收到错误消息

...\python-3.10.2.amd64\lib\site-packages\pytest_cov\plugin.py:308: CovReportWarning: Failed to generate report: No data to report.

A. 对我来说,解决方案是删除选项 '--cov=src' :

[tool.pytest.ini_options]
# Also see
# https://docs.pytest.org/en/6.2.x/customize.html#pyproject-toml
# https://pytest-cov.readthedocs.io/en/latest/config.html
# If you want to see console output from tests, include -s flag
addopts = [
    '--cov-fail-under=98',
    '--cov-report=html:pytest',
    '--cov-report=term-missing',
    '--junitxml=report.xml'
]

B. 我还以为我必须在 pyproject.toml 中包含以下设置:

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

然而,在 pyproject.toml 中包含与否似乎没有什么区别。

C. 为了给PyCharm申请pyproject.toml,我在项目设置下指定了它的路径: Pylint => pylintrc 的路径 (pyproject.toml 没有明确的选项,但它仍然有效。)