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

标签 python testing mocking

<分区>

我在 python 的一些代码中使用了 retry 装饰器。但我想通过消除它的影响来加快我的测试。

我的代码是:

@retry(subprocess.CalledProcessError, tries=5, delay=1, backoff=2, logger=logger)
def _sftp_command_with_retries(command, pem_path, user_at_host):
    # connect to sftp, blah blah blah
    pass

如何在测试时去掉装饰器的效果?我无法创建未修饰的版本,因为我正在测试使用它的更高级别的函数。

由于 retry 使用 time.sleep 退后,理想情况下我可以修补 time.sleep 但因为这是在我不认为这是可能的装饰器。

有什么方法可以加快使用此功能的测试代码?

更新

我基本上是在尝试测试使用它的更高级别的函数,以确保它们捕获 _sftp_command_with_retries 抛出的任何异常。由于 retry 装饰器将传播它们,我需要一个更复杂的模拟。

因此来自 here我可以看到如何模拟装饰器。但现在我需要知道如何编写一个本身就是装饰器的模拟。它需要调用 _sftp_command_with_retries,如果它引发异常,则传播它,否则返回返回值。

在导入我的函数后添加这个不起作用:

_sftp_command_with_retries = _sftp_command_with_retries.__wrapped__ 

最佳答案

retry decorator you are using建立在 decorator.decorator utility decorator 之上如果未安装该软件包,则使用更简单的回退。

结果有一个 __wrapped__ 属性,让您可以访问原始函数:

orig = _sftp_command_with_retries.__wrapped__

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

orig = _sftp_command_with_retries.__closure__[1].cell_contents

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

请注意,decoratorretry 包元数据中被列为依赖项,如果您使用 pip 安装它,decorator 包会自动安装。

您可以使用 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

请注意,您总是可以使用 mock 修补 time.sleep()。装饰器代码将使用模拟,因为它引用了 the module source code 中的“全局”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 - 在 python 中测试时如何删除装饰器的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32697596/

相关文章:

python - 在 eclipse 中导入 matplotlib

python - 在 Intel Mac OS X 上将 PATH AMPL 求解器与 Pyomo 结合使用

php - 模拟测试特征和模拟方法

python - 模拟 flask.request 在 python nosetests

python - 命名空间和类

python - 只使用标准库编写 Python 2 和 3 兼容代码的最佳方法

python-3.x - 如何在 tensorflow 中使用 python3 预测 LSTM 模型中的情绪?

c# - 在外国Windows上测试,这可能吗?

java - 使用 JMockit 模拟抽象类中的非公共(public)静态方法?

python-2.7 - 从上下文管理器创建的对象中模拟函数