Update
This issue was caused by me not including a token in the APIClient's header. This is resolved.
I have a standard ModelViewSet at /test-endpoint
. I am trying to use APIClient to test the endpoint.
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)
This call works with the parameters I set in sample_call. It returns a 201. When I run the test, however, I get a 500. How can I modify this to get the 201 passed?
I run the tests with python src/manage.py test modulename
To rule out the obvious, I copy-pasted the sample call into Postman and run it without issue. I believe the 500 status code is coming from the fact that I'm testing the call and not using it in a live environment.
No error messages are being thrown beyond the AssertionError:
AssertionError: 500 != 201
Full Output of testing
/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'...
The S3 warning is expected. Otherwise, all appears normal.
1 Answers
Answers 1
To debug a failing test case in Django/DRF:
Put
import pdb; pdb.set_trace()
just before the assertion and see therequest.content
as suggested by @Igonato, or you can just add aprint(request.content)
, there is no shame for it.Increase verbosity of your tests by adding
-v 3
Use dot notation to investigate the specific test case:
python src/manage.py test modulename.tests.<TestCase>.<function>
I hope these are useful to keep in mind.
0 comments:
Post a Comment