当前位置: 首页 > news >正文

pytest8.x版本 中文使用文档-------32.示例:使用自定义目录收集器

默认情况下,pytest 使用pytest.Package来收集包含 __init__.py 文件的目录,使用 pytest.Dir来收集其他目录。如果你想要自定义目录的收集方式,你可以编写自己的pytest.Directory 收集器,并使用 pytest_collect_directory钩子来连接它。

对于目录清单文件的一个基本示例

假设你想要自定义每个目录的收集方式。以下是一个conftest.py插件的示例,它允许目录包含一个manifest.json文件,该文件定义了该目录的收集方式。在这个示例中,仅支持一个简单的文件列表,但你可以想象添加其他键,如排除项和通配符。

from __future__ import annotations  import json  import pytest  class ManifestDirectory(pytest.Directory):  def collect(self):  # pytest的标准行为是遍历所有`test_*.py`文件,并对每个文件调用`pytest_collect_file`。  # 这个收集器改为读取`manifest.json`文件,并且仅对其中定义的文件调用`pytest_collect_file`。  manifest_path = self.path / "manifest.json"  manifest = json.loads(manifest_path.read_text(encoding="utf-8"))  ihook = self.ihook  for file in manifest["files"]:  yield from ihook.pytest_collect_file(  file_path=self.path / file, parent=self  )  @pytest.hookimpl  
def pytest_collect_directory(path, parent):  # 对于包含`manifest.json`文件的目录,使用我们的自定义收集器。  if path.joinpath("manifest.json").is_file():  return ManifestDirectory.from_parent(parent=parent, path=path)  # 否则,回退到标准行为。  return None

你可以创建一个 manifest.json 文件和一些测试文件:

{"files": ["test_first.py","test_second.py"]
}
# content of test_first.py
from __future__ import annotationsdef test_1():pass
# content of test_second.py
from __future__ import annotationsdef test_2():pass
# content of test_third.py
from __future__ import annotationsdef test_3():pass

现在你可以执行测试规范了:

customdirectory $ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project/customdirectory
configfile: pytest.ini
collected 2 itemstests/test_first.py .                                                [ 50%]
tests/test_second.py .                                               [100%]============================ 2 passed in 0.12s =============================

请注意,test_three.py 没有被执行,因为它没有在清单中列出。

你可以验证你的自定义收集器是否出现在收集树中:

customdirectory $ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project/customdirectory
configfile: pytest.ini
collected 2 items<Dir customdirectory><ManifestDirectory tests><Module test_first.py><Function test_1><Module test_second.py><Function test_2>======================== 2 tests collected in 0.12s ========================

http://www.lryc.cn/news/412860.html

相关文章:

  • c语言第七天笔记
  • 软件测试经理工作日常随记【8】-UI自动化_加密接口的传输
  • 基于FPGA的出租车计费系统设计---第一版--郝旭帅电子设计团队
  • 商汤联合建工社共同打造“住建领域法规标准知识大模型”
  • 基于STM32的智能交通监控系统教程
  • Git和TortoiseGit的安装与使用
  • 改进YOLOv5:加入非对称卷积块ACNet,加强CNN 的内核骨架,包含VOC对比实验
  • 论文解读(12)-Transfer Learning
  • 力扣高频SQL 50题(基础版)第三十八题
  • 大模型下的视频理解video understanding
  • 【网络安全】CR/LF注入+Race Condition绕过MFA
  • 深度学习入门——卷积神经网络
  • 快团团供货大大团长帮卖团长如何线上结算和支付货款?
  • vite vue3 Webstorm multiple export width the same name “default“
  • Transformer预测模型及其Python和MATLAB实现
  • 草的渲染理论
  • Redis:十大数据类型
  • bugku-web-source
  • 一键生成视频并批量上传视频抖音、bilibili、腾讯(已打包)
  • Python WSGI服务器库之gunicorn使用详解
  • Java编程达人:每日一练,提升自我
  • (35)远程识别(又称无人机识别)(二)
  • 提供三方API接口、调用第三方接口API接口、模拟API接口(一)通过signature签名验证,避免参数恶意修改
  • CDO学习
  • 奥运会Ⅱ---谁会先抢走你的工作?
  • 用Python打造精彩动画与视频,4.3 创建动态文本和字幕
  • spring boot + vue3 接入钉钉实现扫码登录
  • 二叉树构建(从3种遍历中构建)python刷题记录
  • 计算机网络中协议与报文的关系
  • 机器学习 第8章-集成学习