pytest核心组件:pluggy插件系统(三)

本文系《pytest源码剖析》系列内容

正在连载,欢迎关注

图片

3. 插件机制实例分析

写完软件本体之后,继续编写插件

02 编写插件

以一个名为 eggsample-spam 的项目编写插件,

需要创建一个文件,使用 eggsample.hookimpl 装饰 hook 实现即可

# eggsample-spam/eggsample_spam.pyimport eggsample

@eggsample.hookimpldef eggsample_add_ingredients(ingredients):    """Here the caller expects us to return a list."""    if "egg" in ingredients:        spam = ["lovely spam", "wonderous spam"]    else:        spam = ["splendiferous spam", "magnificent spam"]    return spam

@eggsample.hookimpldef eggsample_prep_condiments(condiments):    """Here the caller passes a mutable object, so we mess with it directly."""    try:        del condiments["steak sauce"]    except KeyError:        pass    condiments["spam sauce"] = 42    return "Now this is what I call a condiments tray!"

当然,为了插件能够进行安装,也需要额外创建一个 setup.py

# eggsample-spam/setup.py
from setuptools import setup
setup(    name="eggsample-spam",    install_requires="eggsample",    entry_points={"eggsample": ["spam = eggsample_spam"]},    py_modules=["eggsample_spam"],)

在插件中,实现了两个 hook:

  • eggsample_add_ingredients

  • eggsample_prep_condiments

在软件调用这两个 hook 时,插件中的 hook 实现也会一同执行,仿佛这两个函数之前就在软件中一样。

03

使用插件

使用插件需要先安装插件

pip install -e eggsample-spam

重新执行 eggsample

图片

从结果可以看出,在安装插件之后,原有的执行方式得到了新的结果:

  1. 插件通过 eggsample_add_ingredients 钩子,修改了 self.ingredients 的数据内容

  2. 插件通过 eggsample_prep_condiments 钩子,修改了 condiments_tray 的数据内容

  3. 插件通过 eggsample_prep_condiments 返回值,让软件多输出了一行

这个实例是官方文档中提供的,它体现出了 pluggy 的一些特点:

  1. 插件可以独立开发、维护、安装、更新、卸载

  2. 插件可以被软件自动调用,并且可以访问和修改软件的自身数据

  3. 插件在实现 hook 时,参数和返回值是可以省略,并不要求 hookspec 一模一样

首发于公众号:测试开发研习社

原创不易,喜欢请星标+点赞+在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值