0

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 

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

I'm so inconsiderate. Thank you very much for pointing out this error. I have run tests, but I see errors. They are related to what I specified in the reverse function. How to understand the correctness of URL names?
Np, it happens. Your reverse should be client-list Django has something called namespace in urls.py which is typically allocated for app namespaces (then you'd be able to do client:client-list). You can see a good example of this in cookiecutter-django (go to the curly brace sections). If the solution is correct, please mark it as accepted.
As in in urls.py in your app, you would write app_name = "client" and your project/urls.py would have ` path("users/", include("app.urls", namespace="client")),`.
Thank you for your answer. I did as you advised but I see such error: django.urls.exceptions.NoReverseMatch: Reverse for 'client-list' not found. 'client-list' is not a valid view function or pattern name.. Do you have any ideas?
In your app's urlpatterns, you should need to write the name of the view, so: urlpatterns = [ path('clients', ClientView.as_view(), name="client-list"), path('clients/<int:pk>', ClientView.as_view(), "client-detail"), ]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.