Я хочу протестировать с помощью django, но они не запускаются, и я не понимаю, что я неправ, возвращается следующая ошибка: строка 44, в test_put res = self.view (req) Выполнено 5 тестов в 0,009 с ОТКАЗАНО (отказы = 2). Уничтожение тестовой базы данных для псевдонима 'default' ... Ожидается, что представление ActorViewSet будет вызываться с аргументом ключевого слова URL с именем «pk». Исправьте URL-адрес conf или правильно установите атрибут .lookup_field
в представлении.
My testCase:
from rest_framework.test import APITestCase, APIRequestFactory
from .views import ActorViewSet
class TestActor(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.view = ActorViewSet.as_view({'get': 'list',
'get': 'retrieve',
'post': 'create',
'put': 'update',
'delete': 'destroy'})
self.url = '/actor/'
def test_list(self):
req = self.factory.get(self.url)
res = self.view(req)
self.assertEqual(res.status_code, 200,
'expected http code 200'
.format(res.status_code)
)
def test_get_one(self):
self.get_one_url = '/actor/1'
req = self.factory.get(self.get_one_url, format='json')
res = self.view(req)
self.assertEqual(res.status_code, 200,
'expected http code 200'
.format(res.status_code)
)
def test_create(self):
data = {'movie_title': 'type', 'name': 'TCY', 'awards': 5}
req = self.factory.post(self.url, data, format='json')
res = self.view(req)
self.assertEqual(res.status_code, 201,
'exepted created 201'
.format(res.status_code)
)
def test_put(self):
self.get_one_url = '/actor/1'
data = {'movie_title': 'type', 'name': 'TCY', 'awards': 3}
req = self.factory.put(self.get_one_url, data, format='json')
res = self.view(req)
self.assertEqual(res.status_code, 204,
'expected http code 204'
.format(res.status_code)
)
def test_destroy(self):
self.get_one_url = '/actor/1'
data = {'movie_title': 'type', 'name': 'TCY', 'awards': 3}
req = self.factory.delete(self.get_one_url, data, format='json')
res = self.view(req)
self.assertEqual(res.status_code, 204,
'expected http code 204'
.format(res.status_code)
)
благодарен за ваши комментарии.
trace:
(venv) C:\Users\Admin\PycharmProjects\apiTime>manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.F..F
======================================================================
FAIL: test_del (api.tests.TestActor)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\apiTime\api\tests.py", line 54, in test_del
res = self.view(req)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\mixins.py", line 90, in destroy
instance = self.get_object()
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\generics.py", line 88, in get_object
assert lookup_url_kwarg in self.kwargs, (
AssertionError: Expected view ActorViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
======================================================================
FAIL: test_put (api.tests.TestActor)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\apiTime\api\tests.py", line 44, in test_put
res = self.view(req)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\mixins.py", line 65, in update
instance = self.get_object()
File "C:\Users\Admin\PycharmProjects\apiTime\venv\lib\site-packages\rest_framework\generics.py", line 88, in get_object
assert lookup_url_kwarg in self.kwargs, (
AssertionError: Expected view ActorViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
----------------------------------------------------------------------
Ran 5 tests in 0.009s
FAILED (failures=2)
Destroying test database for alias 'default'...