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

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": "jimmysmith@gmail.com"
        }
        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": "bobmarley@gmail.com"
        }
        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)

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)

Back to Top