python - pytest-cov - 不计算集成测试目录的覆盖率
问题描述
我有以下目录结构:
./
src/
tests/
unit/
integration/
我想使用 pytest 在unit/
和中运行所有测试,但我只想在运行测试时(而不是在运行测试时)计算目录的integration/
覆盖率。src/
unit/
integration/
我现在使用的命令(计算所有测试的覆盖率tests/
):
pytest --cov-config=setup.cfg --cov=src
使用 setup.cfg 文件:
[tool:pytest]
testpaths = tests
[coverage:run]
branch = True
我知道我可以@pytest.mark.no_cover
在集成测试中为每个测试函数添加装饰器,但我更愿意标记整个目录而不是装饰大量函数。
解决方案
您可以动态附加标记。下面的示例在pytest_collection_modifyitems
钩子的自定义 impl 中执行此操作。将代码放在conftest.py
项目根目录中的 a 中:
from pathlib import Path
import pytest
def pytest_collection_modifyitems(items):
no_cov = pytest.mark.no_cover
for item in items:
if "integration" in Path(item.fspath).parts:
item.add_marker(no_cov)
推荐阅读
- html - 在 Heroku 上部署 HTML 文件?
- java - 地图列表按某个键分组并汇总
- javascript - 聚合多个查找的匹配字段
- javascript - 将 div 的内部 HTML 更改为从用户收集的变量
- jestjs - jest-circus 无法重试失败的测试
- android - 如何在 android 中使用超时、间隔和计数设置 TCP 保持活动套接字?
- excel-formula - 使用 Excel 公式将层次结构树转换为父/子表
- android - 更改颜色突出显示的 TextInputLayout 并提高提示的高度
- r - 从非结构化文本分配计数
- apache-kafka - 是否有关于创建 Kafka 接收器或源插件的文档/博客/示例?