如何使用Django的assertJSONEqual来验证返回JsonResponse的视图的响应

Mar*_*mro 20 python django django-testing python-3.x jsonresponse

我正在使用Python 3.4和Django 1.7.我有一个观点回归JsonResponse.

def add_item_to_collection(request):
    #(...)
    return JsonResponse({'status':'success'})
Run Code Online (Sandbox Code Playgroud)

我想验证该视图是否使用单元测试返回正确的响应:

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(response.content, {'status': 'success'})
Run Code Online (Sandbox Code Playgroud)

但该assertJSONEqual()行提出了一个例外:

Error
Traceback (most recent call last):
  File "E:\Projects\collecthub\app\collecthub\collecting\tests.py", line 148, in test_success_when_added_before
    self.assertJSONEqual(response.content, {'status': 'OK'})
  File "E:\Projects\collecthub\venv\lib\site-packages\django\test\testcases.py", line 675, in assertJSONEqual
    data = json.loads(raw)
  File "C:\Python34\Lib\json\__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
Run Code Online (Sandbox Code Playgroud)

当响应包含JSON时,检查响应内容的正确方法是什么?当我尝试比较原始值时,为什么我会出现类型错误assertJSONEqual()

res*_*ate 32

看起来你正在使用Python 3,所以response.content在将它传递给之前你需要变成一个UTF-8编码的字符串self.assertJSONEqual:

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)
        self.assertJSONEqual(
            str(response.content, encoding='utf8'),
            {'status': 'success'}
        )
Run Code Online (Sandbox Code Playgroud)

如果要同时支持Python 2.7和Python 3,请使用sixdjango 附带的兼容性库:

from __future__ import unicode_literals
from django.utils import six

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)

        response_content = response.content
        if six.PY3:
            response_content = str(response_content, encoding='utf8')

        self.assertJSONEqual(
            response_content,
            {'status': 'success'}
        )
Run Code Online (Sandbox Code Playgroud)

  • 这也适用于`str`,而不是`str`:`response.content.decode("utf-8")` (8认同)

tut*_*uju 8

与respondcreate的解决方案类似,您也可以使用Django的force_text(自1.5版开始提供),以实现更短的跨平台解决方案:

from __future__ import unicode_literals
from django.utils.encoding import force_text

class AddItemToCollectionTest(TestCase):

    def test_success_when_not_added_before(self):
        response = self.client.post('/add-item-to-collection')
        self.assertEqual(response.status_code, 200)

        self.assertJSONEqual(force_text(response.content), {'status': 'success'})
Run Code Online (Sandbox Code Playgroud)