如何在Django中正确运行基于APITestCase的测试?

h7wcgrx3  于 2023-01-21  发布在  Go
关注(0)|答案(1)|浏览(166)

当我使用python manage.py test命令时,在控制台中看到这样的结果:Ran 0 tests in 0.000s.
如何运行这些UNIT tests
另外,如何知道我在reverse函数中使用的URL的正确性?

    • 项目/www.example.com:urls.py:
urlpatterns = [
    path('', include('client.urls')),
]
    • 客户端/www.example.com:urls.py:
urlpatterns = [
    path('clients', ClientView.as_view()),
    path('clients/<int:pk>', ClientView.as_view()),
]
    • 客户端/www.example.com: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)
    • 错误:**
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
li9yvcax

li9yvcax1#

您需要为每个测试用例添加前缀test_
例如:

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)

相关问题