jeudi 2 avril 2020

NoReverseMatch exception when creating integration test for REST API with Python Django

I've created a REST API accepting a POST request in Django. The request itself works great when calling from a React front end, there should be nothing wrong with the API itself.

I want to create integration test for the API.

I've written the below test, which is basically copy/paste from the Django Documentation adapted to my API:

from django.urls import reverse, path
from rest_framework import status
from rest_framework.test import APITestCase

    class SendLoginEmailTest(APITestCase):

        def test_post_withValidData_loginEmailIsSent(self):
            url = reverse('send_login_email')
            data = {'email': 'validemail@email.com', 'token': '4d4ca980-cb0c-425d-8d2c-7e38cb33f38e'}
            response = self.client.post(url, data, format='json')
            self.assertEqual(response.status_code, status.HTTP_200_OK)

I run the test using the command:

python3 manage.py test

The test fails with the following exception:

django.urls.exceptions.NoReverseMatch: Reverse for 'send_login_email' not found. 'send_login_email' is not a valid view function or pattern name.

Any ideas what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire