Getting error when running automated test on register APIView using Django Rest Framework testing #150267
Unanswered
remoteconn-7891
asked this question in
API and Webhooks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Select Topic Area
Question
Body
Hello everyone, I’m seeing errors when I run automated tests for my Register APIView API endpoint. I’m building a test with DRF and I think I set up everything correctly, I’m following the DRF docs.
Here is my automated test (test_register.py)
class UserRegisterTests(APITestCase):
def test_register_user(self):
register serializers
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(style={'input_type': 'password'}, write_only=True)
views.py
API endpoint for registration
@authentication_classes([JWTAuthentication])
class RegisterView(APIView):
def post(self, request):
serializer = RegisterSerializer(data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
Finally here is the error traceback
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
FAIL: test_register_user (arborfindr.test.test_register.UserRegisterTests.test_register_user)
Traceback (most recent call last):
File "/home/coreyj/Documents/ArborHub/MyProject/arborfindr/test/test_register.py", line 12, in test_register_user
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 400 != 201
Ran 1 test in 0.015s
Here is the link to the example I followed for testing with DRF Testing - Django REST framework.
Please I need help with this, in the docs the Response is set to status.HTTP_201_CREATED but not sure if I should change the status code.
Please let me know if you need anything else from me.
Beta Was this translation helpful? Give feedback.
All reactions