如何在单元测试中使用 django 序列化程序

Posted

技术标签:

【中文标题】如何在单元测试中使用 django 序列化程序【英文标题】:How to use django serializer in unit test 【发布时间】:2016-11-22 02:46:52 【问题描述】:

类 ProcessDocRequestTestCase(BxApiTestCase): 用户名 = 'hr'

def test_email_sent_on_creation(self):
    r = r0 = self.r('post', 201, '/api/processes', %post_data%)

    requestor_superior_id = r0.data['extra']['next_employees'][0]
    superior_emp = Employee.objects.get(pk=requestor_superior_id)
    employee_serializer = EmployeeSerializer(superior_emp, context='request': r)
    employee_serializer.data

最后一行因此错误而崩溃:

Traceback(最近一次调用最后一次):文件 “/my_proj/bx/process/tests.py”,第 81 行,在 test_email_sent_on_creation employee_serializer.data 文件“/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py”, 第 503 行,在数据中 ret = super(Serializer, self).data 文件 "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py", 第 239 行,在数据中 self._data = self.to_representation(self.instance) 文件“/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/serializers.py”, 第 472 行,在 to_representation 中 ret[field.field_name] = field.to_representation(属性)文件“/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/relations.py”, 第 340 行,在 to_representation 中 url = self.get_url(值,self.view_name,请求,格式)文件“/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/relations.py”, 第 277 行,在 get_url return self.reverse(view_name, kwargs=kwargs, request=request, format=format) 文件 "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/reverse.py", 第 50 行,反向 url = _reverse(viewname, args, kwargs, request, format, **extra) 文件 "/my_proj/venv/local/lib/python2.7/site-packages/rest_framework/reverse.py", 第 65 行,在 _reverse 中 return request.build_absolute_uri(url) AttributeError: 'Response' object has no attribute 'build_absolute_uri'

我该怎么做?

作为参考,我的 r 方法是这样定义的:

from rest_framework.test import APIClient
..

def setUp(self):
    self.refreshTestData()
    self.c = APIClient()
    if getattr(self, 'username', None):
        self.login(self.username, self.password)

def r(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')
    r = getattr(self.c, method)(url, *args, **kwargs)
    self.assertEqual(r.status_code, status_code,
        'Expected status %d, got %d.\n%s\n%s' % (
            status_code, r.status_code, url, pformat(getattr(r, 'data', None))) )
    return r

【问题讨论】:

【参考方案1】:

为此,我只需要使用APIRequestFactory“模拟”一个请求。

所以我也写了这个方法:

# call response
def r(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')


    r = getattr(self.c, method)(url, *args, **kwargs)
    self.assertEqual(r.status_code, status_code,
        'Expected status %d, got %d.\n%s\n%s' % (
            status_code, r.status_code, url, pformat(getattr(r, 'data', None))) )
    return r

# return both request and response
def rq(self, method, status_code, url, data=None, *args, **kwargs):
    kwargs['data'] = data
    kwargs['format'] = kwargs.get('format', 'json')

    request = getattr(self.f, method)(url, *args, **kwargs)

    return request, self.r(method, status_code, url, data)

然后我就是这样称呼它的:

    rq, rs = self.rq('post', 201, data)
    rs0 = rs

    requestor_superior_id = rs0.data['extra']['next_employees'][0]
    superior_emp = Employee.objects.get(pk=requestor_superior_id)

    self.attach_user_to_request(rq, self.username)

    employee_serializer = EmployeeSerializer(superior_emp, context='request': rq)
    employee_serializer.data  # works

注意:我还必须像这样将用户“附加”到请求中

def attach_user_to_request(self, request, username, password=None):
    try:
        user = User.objects.get(email=username+'@test.test')
    except User.DoesNotExist:
        raise Exception('No Such User')
    if not user.check_password(None, password or 'test'):
        raise Exception('Bad Password')
    user.backend = settings.AUTHENTICATION_BACKENDS[0]
    request.user = user
    return True

这将在 强制身份验证 here

下讨论

【讨论】:

以上是关于如何在单元测试中使用 django 序列化程序的主要内容,如果未能解决你的问题,请参考以下文章

如何将 django 单元测试分布在多个文件上?

如何对 django 消息进行单元测试?

如何在 Django 中使用不同的设置进行单元测试?

如何在 django 中对文件上传进行单元测试

如何对 django url 进行单元测试?

如何在 Django 单元测试中获取请求对象?