1

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
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
happysachan
  • 131
  • 1
  • 7

1 Answers1

0

Found a way:

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
happysachan
  • 131
  • 1
  • 7