您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在python中进行测试时,如何删除装饰器的效果?

在python中进行测试时,如何删除装饰器的效果?

retry您使用的装饰是建立在顶部decorator.decorator实用的装饰,如果未安装包一个简单的回退。

结果具有一个__wrapped__属性,可让您访问原始功能

orig = _sftp_command_with_retries.__wrapped__

如果decorator未安装, 并且 您使用的是3.2之前的Python版本,则该属性将不存在;您必须手动进入装饰器闭合:

orig = _sftp_command_with_retries.__closure__[1].cell_contents

(索引0处的闭包是retry_decorator调用retry()自身时产生的)。

需要注意的是decorator被列为依赖retry包的元数据,如果你安装它pipdecorator包会被自动安装。

您可以使用try...except

try:
    orig = _sftp_command_with_retries.__wrapped__
except AttributeError:
    # decorator.decorator not available and not Python 3.2 or newer.
    orig = _sftp_command_with_retries.__closure__[1].cell_contents

请注意,您 始终 可以time.sleep()使用模拟补丁。装饰器代码将使用该模拟程序,因为它引用了模块源代码中的“全局”time模块。

或者,您可以修补retry.api.__retry_internal

import retry.api
def dontretry(f, *args, **kw):
    return f()

with mock.patch.object(retry.api, '__retry_internal', dontretry):
    # use your decorated method

这会将执行实际重试的功能临时替换为直接调用原始功能功能

python 2022/1/1 18:36:43 有239人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶