Overriding decorator during unit test in python

拥有回忆 提交于 2019-11-30 14:00:40

I think your gonna have to get into the dark underworld of Mock, but once you get your head around it (if you haven't already) the dark underworld turns into a bright blue heavenly sky of mockiness.

You could use the patch module of Mock to to patch this decorator so your views using it can become more testable: http://www.voidspace.org.uk/python/mock/patch.html. Personally I have not tried mocking a decorator before but it should work...

@patch('python.path.to.decorator', new_callable=PropertyMock)
def my_test(self, decorator_mock):
    # your test code

Give that a whirl.

You can read about the patch module in Mock here: http://www.voidspace.org.uk/python/mock/patch.html

edit

new_callable=PropertyMock is probably not the right thing to do for patching a decorator.

Perhaps try:

@patch('python.path.to.decorator', lambda: func: func)
def my_test(self):
    # your test code

This should in theory patch the decorator so it just returns the function back rather than does all the stuff you have in wrapped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!