在Python单元测试期间覆盖装饰器 [英] Overriding decorator during unit test in python

查看:93
本文介绍了在Python单元测试期间覆盖装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在装饰一个基于django类的视图.不幸的是,装饰器进行外部调用以进行状态检查,这超出了单元测试的范围,因此我想覆盖装饰器,使其在单元测试期间不执行任何操作.这是我的装饰器:

I have a django class based view that I'm decorating. Unfortunately that decorator makes outside calls to do status checks which is outside the scope of what the unit test should do so I want to override the decorator to do nothing during my unit tests. Here is my decorator:

decorators.py

decorators.py

def status_check(func):
    @wraps(func)
    def wrapped(request, *args, **kwargs):

        uri = settings.SERVER_URI
        status_code = None
        bad_status = [404, 500]

        try:
            response = requests.head(uri)
        except requests.ConnectionError as err:
            LOGGER.error('Server is hosed! Oh Noes! Error: %s ' % (err))
            raise Http404
        except Exception as err:
            LOGGER.error('Some crazy stuff is happening. Its Bad. '
                         'Error: %s' % (err))
            raise Http404

        status_code = response.status_code
        if not status_code or status_code in bad_status:
            LOGGER.error('Awww Snap! Server is not happy: %s' % (status_code))
            raise Http404
        return func(request, *args, **kwargs)
    return wrapped

views.py

class HandleFoo(DetailView):
    @method_decorator(status_check)
    def post(self, request):
        # do stuff here

tests.py

class RunTest(TestCase):
    def test_post(self):
        post_data = json.dumps({'stuff': 'vodka', 'things': 'tonic'})
        resp = self.client.post(self.foo_uri,
                                post_data,
                                content_type='application/json',
                               )
        self.assertEqual(resp.status_code, 200)

那么,有没有办法让我覆盖装饰器,或者我可以完全绕过它?我对此很困惑.

So is there a way for me to either override the decorator or can I bypass it altogether? I'm rather stumped on this.

编辑 尝试使用krak3n中的以下内容模拟请求:

EDIT Tried mocking out the request using the following from krak3n:

@patch('app.views.method_decorator.status_check', lambda func: func)
@patch('app.views.status_check', lambda func: func)
@patch('app.decorators.status_check', lambda func: func)
@patch('app.views.HandleFoo.post', lambda func: func)

最后一个方法使我到目前为止最接近,但是最终抛出了堆栈跟踪:

The last method gets me the closest thus far, but it ends up throwing a stacktrace:

Traceback (most recent call last):
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/mock.py", line 1201, in patched
    return func(*args, **keywargs)
  File "/Users/squiddly/projects/tests/app/tests.py", line 165, in test_post
    content_type='application/json',
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/client.py", line 463, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/client.py", line 297, in post
    return self.request(**r)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/client.py", line 406, in request
    response = self.handler(environ)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/client.py", line 111, in __call__
    response = self.get_response(request)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/core/handlers/base.py", line 224, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/views/defaults.py", line 41, in server_error
    return http.HttpResponseServerError(template.render(Context({})))
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/utils.py", line 65, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
    return compiled_parent._render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/utils.py", line 65, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/loader_tags.py", line 156, in render
    return self.render_template(self.template, context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/loader_tags.py", line 138, in render_template
    output = template.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/test/utils.py", line 65, in instrumented_test_render
    return self.nodelist.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/Users/squiddly/envs/testenv/lib/python2.7/site-packages/django/template/base.py", line 1185, in render
    _dict = func(*resolved_args, **resolved_kwargs)
  File "/Users/squiddly/projects/tests/app/templatetags/app_extras.py", line 40, in get_data
    if request.session.has_key('start_time'):
AttributeError: 'str' object has no attribute 'session'

推荐答案

我认为您将不得不进入Mock的黑暗黑社会,但是一旦您掌握了它(如果尚未解决),黑暗的黑社会变成了明亮的蓝天,充满了嘲讽.

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.

您可以使用Mockpatch模块来修补此装饰器,以便使使用它的视图更具可测试性:

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

打个招呼.

您可以在Mock中阅读有关patch模块的信息: http://www.voidspace.org.uk/python/mock/patch.html

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

new_callable=PropertyMock可能不是修补装饰器的正确方法.

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

也许尝试:

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

从理论上讲,这应该修补装饰器,以便它只返回函数,而不是返回您在wrapped中拥有的所有东西.

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

这篇关于在Python单元测试期间覆盖装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆