如何模拟一个装饰函数

How to mock a decorated function(如何模拟一个装饰函数)
本文介绍了如何模拟一个装饰函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

出于测试原因,我需要能够模拟在其他地方使用的装饰的内部/原始功能:

For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else:

在 mydecorator.py 中:

In mydecorator.py:

def my_decorator(f):
    def wrapped_f():
        print "decorated"
        f()
    return wrapped_f


@my_decorator
def function_to_be_mocked():
    print 'original'


def function_to_be_mocked_undecorated():
    print 'original'


def run_decorated():
    function_to_be_mocked()


def run_undecorated():
    decorated_funtion = my_decorator(function_to_be_mocked_undecorated)
    decorated_funtion()

如您所见,我有几个版本的原始函数 function_to_be_mocked,一个带有装饰器 my_decorator,另一个是裸".运行器函数 run_decorated() 调用 function_to_be_mocked 的修饰版本, run_undecorated() 调用未修饰版本并手动"应用修饰器.两者的结果是一样的:

As you can see, I have several versions of the original function function_to_be_mocked, one with the decorator my_decorator and one 'naked'. The runner function run_decorated() calls the decorated version of function_to_be_mocked and run_undecorated() calls the undecorated version and applies the decorator 'manually'. The result of both are the same:

decorated
original

现在我想测试 runner 函数,但我需要模拟原始函数 function_to_be_mocked 并且模拟的版本应该被装饰:

Now I want to test the runner function but I need to mock the original function function_to_be_mocked but also the mocked version should be decorated:

import unittest
import mydecorator
from mock import patch

def mock_function():
    print 'mockified'

class Test(unittest.TestCase):

    @patch('mydecorator.function_to_be_mocked_undecorated')
    def test_undecorated_mocked(self, mock_function_to_be_mocked_undecorated):
        mydecorator.function_to_be_mocked_undecorated = mock_function
        mydecorator.run_undecorated()
        assert 1==0

    @patch('mydecorator.function_to_be_mocked')
    def test_decoratorated_mocked(self, mock_function_to_be_mocked):
        mydecorator.function_to_be_mocked = mock_function
        mydecorator.run_decorated()
        assert 1==0

这对于未修饰的版本 test_undecorated_mocked 可以正常工作:

This works as expected for the undecorated version test_undecorated_mocked:

decorated
mockified

但装饰版给出:

mockified

所以装饰器消失了.

是否可以让装饰版本以与未装饰版本相同的方式工作,其中手动"应用装饰器?

Is it possible to get the decorated version working in the same way as the undecorated version, where the decorator is applied 'manually'?

我尝试在装饰器中暴露内部函数,但没有成功.

I tried to expose the inner function in the decorator without success.

我看到了这个问题 你如何模拟一个在单元测试中应用了装饰器的函数?但这对我没有帮助.

I saw this question How do you mock a function which has decorator apply to it in a unit test? but this doesn't help me.

推荐答案

Python在加载模块时应用装饰器,所以在test_decoratorated_mocked<中设置function_to_be_mockedmock_function/code> 确实会将该函数更改为未修饰的函数.

Python applies the decorator when loading the module so setting function_to_be_mocked to mock_function in test_decoratorated_mocked would indeed change that function into an undecorated function.

如果要模拟 function_to_be_mocked,则需要再次手动添加装饰器:

You'd need to manually add the decorator again if you wish to mock function_to_be_mocked:

mydecorator.function_to_be_mocked = mydecorator.my_decorator(mock_function)

这篇关于如何模拟一个装饰函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)