問題描述
出于測試原因,我需要能夠模擬在其他地方使用的裝飾的內部/原始功能:
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_mocked
為mock_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)
這篇關于如何模擬一個裝飾函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!