I want to unit test multiple decorated functions' decorator factory inputs.
I have tried mocking the decorator factory, but this did not help me associate the particular decorator factory call to a specific decorated function. Instead I just get a list of decorated factory calls in the order it is called in the file.
# 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
I want something like this:
#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
The closest I have come is below. (Based on this answer https://stackoverflow.com/a/37890916/8196029 .) This is not what I want because it is just a list of calls not associated to the decorated function names:
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