我想对多个装饰函数的装饰器工厂输入进行单元测试。
我尝试模拟装饰器工厂,但这并没有帮助我将特定装饰器工厂调用与特定的装饰函数关联。相反,我只是按照文件中被调用的顺序获取经过修饰的工厂调用的列表。
# decorator.py
from decorator_def import decorator_factory
@decorator_factory(2)
def say_goodbye():
print 'goodbye'
@decorator_factory(4)
def say_what():
print 'what'
import functools
def decorator_factory(input=1):
def decorator(func):
@functools.wraps(func)
def say_hello(*_args):
print 'hello '*input
_result = func(*_args)
return _result
return say_hello
return decorator
我想要这样的东西:
#pseudo
def test_decorated_say_goodbye():
assert decorator_factory_input_for_say_goodbye == 2
def test_decorated_say_what():
assert decorator_factory_input_for_say_what == 4
我最近来的是下面。 (基于此答案https://stackoverflow.com/a/37890916/8196029。) 这不是我想要的,因为它只是与修饰的函数名称无关的调用列表:
import unittest
import imp
from mock import patch, MagicMock, call
import decorator
class TestDecorated(unittest.TestCase):
def setUp(self):
def kill_patches():
patch.stopall()
imp.reload(decorator)
self.addCleanup(kill_patches)
self.mocked_df = MagicMock()
patch('decorator_def.decorator_factory', self.mocked_df).start()
imp.reload(decorator)
def test_decorated_order(self):
df_call_args_expected = [
call(2), # say_goodbye
call(4) # say_what
]
assert self.mocked_df.call_args_list == df_call_args_expected
答案 0 :(得分:0)
找到了一种方法:
from decorator import say_what, say_goodbye
def test_decorated_say_what():
assert say_what.__code__.co_freevars[1] == 'input'
assert say_what.__closure__[1].cell_contents == 4
def test_decorated_say_goodbye():
assert say_goodbye.__code__.co_freevars[1] == 'input'
assert say_goodbye.__closure__[1].cell_contents == 2