【问题标题】:using APITestCase with django-rest-framework将 APITestCase 与 django-rest-framework 一起使用
【发布时间】:2015-03-29 02:15:53
【问题描述】:

我遵循了这个代码:

from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

class AccountTests(APITestCase):
    def test_create_account(self):
        """
        Ensure we can create a new account object.
        """
        url = reverse('account-list')
        data = {'name': 'DabApps'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, data)

在此处的 django-rest-framewok 文档中找到:

DRF API-guide: Testing example

我创建了一个带有单个字段 name 的单个 Model,但我仍然收到“错误请求 400 错误”。视图和reverse 名称也设置正确,我已经手动测试成功查看URL。我没有启用身份验证

不知道我是否遗漏了一步?

有人有django-rest-framework APITestCase create model object 测试代码sn-p 的工作示例吗?

【问题讨论】:

  • 您是否尝试过打印response.errors 或response.data?
  • @meshy,我最终只是按照下面的 git repo 来让它工作。谢谢。

标签: python django rest testing django-rest-framework


【解决方案1】:

可能是 JSON 解码错误。

在self.client.post(url, data, format='json') 行中使用json.dumps(data) 并尝试。

【讨论】:

    【解决方案2】:

    这个GIT repo 有几个工作示例,我能够遵循并让APITestCase 工作:

    django-rest-framework-oauth2-provider-example/apps/users/tests.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-02
      • 2018-03-23
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2018-07-13
      相关资源
      最近更新 更多