Pytest 插件:pytest_runtest_protocol

Hook 方法之 pytest_runtest_protocol:

pytest_runtest_protocol :

官方给的解释是:为给定的测试项目执行runtest_setup / call / teardown协议;

可以在执行每个测试用例的各个阶段进行钩子拦截,比如在测试用例开始执行前、执行中间某一步骤、执行完成后等,从而可以方便地进行一些额外的操作或者扩展功能。

pytest 的默认 pytest_runtest_protocol 实现会按顺序执行以下操作:

  1. 调用 pytest_runtest_logstart 钩子,标记测试开始
  2. 调用 pytest_runtest_setup 钩子,执行测试前置条件
  3. 调用 pytest_runtest_call 钩子,执行测试函数本身
  4. 调用 pytest_runtest_teardown 钩子,执行测试后置清理
  5. 调用 pytest_runtest_logfinish 钩子,标记测试结束

The default runtest protocol is this (see individual hooks for full details):

  • pytest_runtest_logstart(nodeid, location)

  • Setup phase:

    • call = pytest_runtest_setup(item) (wrapped in CallInfo(when="setup"))

    • report = pytest_runtest_makereport(item, call)

    • pytest_runtest_logreport(report)

    • pytest_exception_interact(call, report) if an interactive exception occurred

  • Call phase, if the setup passed and the setuponly pytest option is not set:

    • call = pytest_runtest_call(item) (wrapped in CallInfo(when="call"))

    • report = pytest_runtest_makereport(item, call)

    • pytest_runtest_logreport(report)

    • pytest_exception_interact(call, report) if an interactive exception occurred

  • Teardown phase:

    • call = pytest_runtest_teardown(item, nextitem) (wrapped in CallInfo(when="teardown"))

    • report = pytest_runtest_makereport(item, call)

    • pytest_runtest_logreport(report)

    • pytest_exception_interact(call, report) if an interactive exception occurred

  • pytest_runtest_logfinish(nodeid, location)

Parameters:

  • item (Item) -- Test item for which the runtest protocol is performed.

  • nextitem (Item| None) -- The scheduled-to-be-next test item (or None if this is the end my friend).

    def pytest_runtest_protocol(item, nextitem):
    print("\n[pytest_generate_protocol]这个钩子函数在每个测试用例开始和结束时被调用",item.name)

python 复制代码
def pytest_runtest_protocol(item, nextitem):
    # 自定义测试开始逻辑
    item.ihook.pytest_runtest_logstart(nodeid=item.nodeid, location=item.location)
    
    # 自定义测试执行流程
    item.ihook.pytest_runtest_setup(item=item)
    item.ihook.pytest_runtest_call(item=item)
    item.ihook.pytest_runtest_teardown(item=item, nextitem=nextitem)
    
    # 自定义测试结束逻辑
    item.ihook.pytest_runtest_logfinish(nodeid=item.nodeid, location=item.location)
    return True
 

常见应用场景

  1. 性能监控:在协议中插入时间记录逻辑,统计每个测试用例的执行时长。
  2. 动态跳过测试:根据运行时条件(如环境变量)决定是否跳过某些测试。修改测试执行顺序或逻辑。
  3. 日志增强:在测试执行前后输出详细日志,便于调试。
  4. 在特定条件下跳过某些测试步骤 集成自定义报告生成机制。
  5. 实现特殊的测试环境管理。

注意事项

自定义实现时需要确保正确处理所有必要的钩子调用,返回 True 表示测试协议已处理,False 表示交由其他实现处理 避免在钩子中引入过多复杂逻辑,以免影响测试性能 确保正确处理异常情况,避免影响后续测试执行。

自定义实现示例

以下是一个自定义 pytest_runtest_protocol 的示例,通过重写钩子添加日志功能:

复制代码
def pytest_runtest_protocol(item, nextitem):
    # 自定义前置操作
    print(f"Starting test: {item.name}")
    
    # 调用默认的测试协议(包括 setup/call/teardown)
    result = pytest_runtest_protocol.__wrapped__(item, nextitem) if hasattr(pytest_runtest_protocol, '__wrapped__') else None
    
    # 自定义后置操作
    print(f"Finished test: {item.name}")
    return result

注意事项

  • 修改 pytest_runtest_protocol 需谨慎,避免破坏默认流程导致测试失败。
  • 建议优先通过标准钩子(如 pytest_runtest_setup)实现扩展,而非直接重写协议。
  • 使用 pytest_runtest_logstartpytest_runtest_logfinish 等辅助钩子可以减少侵入性。

通过合理利用此协议,可以高度灵活地控制 pytest 的测试行为,满足复杂项目的需求。

相关推荐
吴佳浩3 分钟前
Python入门指南(七) - YOLO检测API进阶实战
人工智能·后端·python
liliangcsdn36 分钟前
python下载并转存http文件链接的示例
开发语言·python
大、男人1 小时前
python之Starlette
python·uvicorn
小智RE0-走在路上3 小时前
Python学习笔记(11) --数据可视化
笔记·python·学习
历程里程碑3 小时前
hot 206
java·开发语言·数据结构·c++·python·算法·排序算法
Coder_Boy_3 小时前
Java+Proteus仿真Arduino控制LED问题排查全记录(含交互过程)
java·人工智能·python
qq_356196953 小时前
day47_预训练模型与迁移学习@浙大疏锦行
python
Tipriest_4 小时前
C++ 的 ranges 和 Python 的 bisect 在二分查找中的应用与实现
c++·python·算法·二分法
老歌老听老掉牙4 小时前
符号计算中的表达式等价性验证:数学等价性与计算简化策略分析
python·数学建模·sympy
scan7244 小时前
向量数据库检索
python