测试Django REST框架帖子尽管有呼叫工作,但仍返回500



update

这个问题是由我不包括Apiclient标题中的令牌引起的。这是解决的。


我在/test-endpoint上有一个标准模型视图。我正在尝试使用apiclient测试端点。

from rest_framework.test import APIClient
... # During this process, a file is uploaded to S3. Could this cause the issue? Again, no errors are thrown. I just get a 500.
self.client = APIClient()
...
sample_call = {
    "name": "test_document",
    "description": "test_document_description"
}
response = self.client.post('/test-endpoint', sample_call, format='json')
self.assertEqual(response.status_code, 201)

此调用与我在sample_call中设置的参数一起使用。它返回201。当我运行测试时,我会得到500。如何修改它以使201通过?

我使用python src/manage.py test modulename

运行测试

为了排除显而易见的内容,我将示例调用委托给了邮递员,然后毫无问题地运行它。我相信500个状态代码来自我正在测试呼叫而不是在实时环境中使用它的事实。


没有错误消息超出断言:

assertionError:500!= 201

测试的完整输出

/home/bryant/.virtualenvs/REDACTED/lib/python3.4/site- packages/django_boto/s3/shortcuts.py:28: RemovedInDjango110Warning:  Backwards compatibility for storage backends without support for the     `max_length` argument in Storage.get_available_name() will be removed in Django 1.10.
s3.save(full_path, fl)
F
======================================================================
FAIL: test_create (sample.tests.SampleTestCase)
Test CREATE Document
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/bryant/api/redacted/src/sample/tests.py", line 31, in test_create
self.assertEqual(response.status_code, 201)
AssertionError: 500 != 201
----------------------------------------------------------------------
Ran 1 test in 2.673s
FAILED (failures=1)
Destroying test database for alias 'default'...

预计S3警告。否则,一切似乎正常。

在django/drf中调试失败的测试用例:

  • import pdb; pdb.set_trace()放在断言之前,请参阅@igonato建议的request.content,或者您只需添加print(request.content),就不会感到羞耻。

  • 通过添加-v 3

  • 来增加测试的详细性。
  • 使用点符号调查特定的测试案例:python src/manage.py test modulename.tests.<TestCase>.<function>

我希望这些都可以牢记。

最新更新