http://stackoverflow.com/questions/7667567/can-i-patch-a-python-decorator-before-it-wraps-a-function
Decorators are applied at function definition time. For most functions, this is when the module is loaded. (Functions that are defined in other functions have the decorator applied each time the enclosing function is called.)
So if you want to monkey-patch a decorator, what you need to do is:
- Import the module that contains it
- Define the mock decorator function
- Set e.g.
module.decorator = mymockdecorator
- Import the module(s) that use the decorator, or use it in your own module
Let me give an exmple:
Here is a decorator function(user_passes_test) in the following file path
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py
If you have a function using this decorator and need to write the test case for it as follows:
@user_passes_test(filter_root, login_url=settings.LOGIN_PAGE) def my_dashboard_view(request):Then, you can put your patch code in the beginning of the tests python file like this:
def mock_function(test_func, login_url=None, redirect_field_name=None): def wrapped_f(view_func): @wraps(view_func, assigned=available_attrs(view_func)) def decorator_view(request, *args, **kwargs): return view_func(request, *args, **kwargs) return decorator_function return wrapped_f from django.contrib.auth.decorators import user_passes_test user_passes_test = mock_function
After doing so, the decorator: user_passes_test() won't infect your test case any more~