问题描述
workspace/
|- .venv/ # virtual environment
|- .vscode/ # ide settings
|- my.repo.folder/ # git repo. Note: these have periods in their names
|- coverage/
|- .report # data file
|- html/ # folder for HTML report
|- service / # service package folder
|- __init__.py
|- ...
|- tests/ # tests folder
|- test_a.py
|- test_b.py
|- ...
|- conftest.py # pytest configuration
|- pyproject.toml # project configuration
|- handler.py # AWS Lambda entry
通常,我们有一个工作区文件夹,我们会将单个存储库克隆到其中。在顶层有工作区配置、虚拟环境等,以及 repo 本身。在 repo 中,我们有一个 service
文件夹作为 python 包的所有源代码和一个 handler.py
文件作为入口点(这是一个 AWS Lambda 函数)。此外,我们将诗歌用于所有打包和依赖项管理,因此项目配置存储在 pyproject.toml
中。我在下面包含了配置设置。
[tool.pytest.ini_options]
addopts = "-v"
testpaths=[
"tests"
]
log_cli = true
log_cli_level = "INFO"
log_file = "logs/pytest.log"
log_file_level = "DEBUG"
markers = []
[tool.coverage.run]
source = ['service']
command_line = '-m pytest tests'
dynamic_context = "test_function"
data_file = 'coverage/.report'
[tool.coverage.report]
show_missing = true
[tool.coverage.html]
show_contexts = true
directory = 'coverage/html'
问题在于,当 source = ['service']
时,Coverage 按预期工作,但不包括核心 handler.py
模块。当我用 include = ['*/service/**','*/handler.py']
替换该行时,coverage.py 没有捕获任何内容。事实上,如果我完全省略该行,则默认情况下不会包含 service
甚至 handler.py
中的任何内容。报告的唯一模块来自 CWD 外部的 .venv
(超级混乱)、conftest.py
和 tests
内的测试模块。我的直觉是,如果我可以在 service
选项中指定 source
,那么覆盖范围至少应该覆盖其默认路径中的那个文件夹,对吗?
有人可以帮我解决如何获得覆盖以报告 handler.py
模块和 service
包的问题吗?
解决方法
尝试使用:
source = ['service','handler']