def tinsel ( to_patch , module_name , decorator = mock_decorator ) : def fn_decorator ( function ) : def wrapper ( * args , * * kwargs ) : with patch ( to_patch , decorator ) : m = importlib . import_module ( module_name ) reload ( m ) function ( * args , * * kwargs ) reload ( m ) return wrapper return fn_decorator
python mock patch only in function
Decorator for simple in - place decorator mocking for tests
def tinsel(to_patch, module_name, decorator=mock_decorator):
    """
    Decorator for simple in-place decorator mocking for tests

    Args:
        to_patch: the string path of the function to patch
        module_name: complete string path of the module to reload
        decorator (optional): replacement decorator. By default a pass-through
            will be used.

    Returns:
        A wrapped test function, during the context of execution the specified
        path is patched.

    """
    def fn_decorator(function):
        def wrapper(*args, **kwargs):
            with patch(to_patch, decorator):
                m = importlib.import_module(module_name)
                reload(m)
                function(*args, **kwargs)

            reload(m)
        return wrapper
    return fn_decorator