Home > Net >  How to properly run tests based on the APITestCase in Django?
How to properly run tests based on the APITestCase in Django?

Time:01-21

When I use the python manage.py test command, in the console I see such a result: Ran 0 tests in 0.000s.

How to run these UNIT tests?

Also how to know the correctness of URLs which I use in the reverse function?

project/urls.py:

urlpatterns = [
    path('', include('client.urls')),
]

client/urls.py:

urlpatterns = [
    path('clients', ClientView.as_view()),
    path('clients/<int:pk>', ClientView.as_view()),
]

client/tests.py:

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from client.models import Client


class ClientTestCase(APITestCase):
    def setUp(self):
        self.data = {
            "first_name": "Jimmy",
            "last_name": "Smith",
            "email": "[email protected]"
        }
        self.response = self.client.post(
            reverse('client:client-list'),
            self.data,
            format="json"
        )

    def create_client(self):
        self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Client.objects.count(), 1)
        self.assertEqual(Client.objects.get().first_name, 'Jimmy')

    def get_clients(self):
        response = self.client.get(reverse('client:client-list'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Client.objects.count(), 1)

    def get_client(self):
        client = Client.objects.get()
        response = self.client.get(
            reverse('client:client-detail', kwargs={'pk': client.id}),
            format="json"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertContains(response, client)

    def update_client(self):
        client = Client.objects.get()
        new_data = {
            "first_name": "Bob",
            "last_name": "Marley",
            "email": "[email protected]"
        }
        response = self.client.put(
            reverse('client:client-detail', kwargs={'pk': client.id}),
            data=new_data,
            format="json"
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Client.objects.get().first_name, 'Bob')

    def delete_client(self):
        client = Client.objects.get()
        response = self.client.delete(
            reverse('client:client-detail', kwargs={'pk': client.id}),
            format="json"
        )
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(Client.objects.count(), 0)

ERROR:

Error
Traceback (most recent call last):
  File "/Users/nurzhan_nogerbek/PycharmProjects/c02_tit/venv/lib/python3.9/site-packages/django/urls/base.py", line 71, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'person'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nurzhan_nogerbek/PycharmProjects/project/person/tests.py", line 15, in setUp
    reverse('person:person-list'),
  File "/Users/nurzhan_nogerbek/PycharmProjects/project/venv/lib/python3.9/site-packages/django/urls/base.py", line 82, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'person' is not a registered namespace

CodePudding user response:

You need to have each test case prefixed by test_

For instance:

    def test_create_client(self):
        self.assertEqual(self.response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Client.objects.count(), 1)
        self.assertEqual(Client.objects.get().first_name, 'Jimmy')

    def test_get_clients(self):
        response = self.client.get(reverse('client:client-list'))
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Client.objects.count(), 1)

  • Related