pytest 测试框架学习(12):pytest.deprecated_call

时间:2022-07-24
本文章向大家介绍pytest 测试框架学习(12):pytest.deprecated_call,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

pytest.deprecated_call

含义

deprecated_call: 是一个上下文管理器,可确保代码块触发 DeprecationWarning 或者 PendingDeprecationWarning; 源码:

deprecated_call 可以将函数当做参数传入,后面跟上参数。

使用

  1. 函数不传参
import warnings
def api_call_v2():
	warnings.warn('use v3 of this api', DeprecationWarning)
	return 200
def test_deprecated_call():
	with deprecated_call():
		assert api_call_v2() == 200
  1. 函数传参,并当做参数传入 deprecated_call 中
import warnings
from pytest import deprecated_call

def api_call_v2(*args, **kwargs):
    warnings.warn('use v3 of this api', DeprecationWarning)
    return kwargs.get("b")

def test_deprecated_call():
    assert deprecated_call(api_call_v2, 123, 456, a=1, b=2, c=3) == 2

这个函数最主要的其实是将被标记了 DeprecationWarning 或者 PendingDeprecationWarning 的函数能够正常被调用,不会在执行时出现 相应的 warning 信息。

直接调用,弹出警告:

使用 deprecated_call,没有警告显示:

说明:本篇参考官网并加入自己些许理解翻译而来,觉得有用,可以点赞和赞赏哦(^ v ^),谢谢支持;如果有不足地方,可留言评论。后续将继续更新。