微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用coverage.py

如何解决使用coverage.py

我有一个使用以下最小结构设置的项目:

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.pytests 内的测试模块。我的直觉是,如果我可以在 service 选项中指定 source,那么覆盖范围至少应该覆盖其认路径中的那个文件夹,对吗?

有人可以帮我解决如何获得覆盖以报告 handler.py 模块和 service 包的问题吗?

解决方法

尝试使用:

source = ['service','handler']

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。