Hello, I’m very new to writing test cases and I’m trying to write one to test my view. However, I keep making mistakes and now this new approach isn’t working.
This is my view:
from rest_framework import generics, filters from todo import models from .serializers import TaskSerializer from django_filters.rest_framework import DjangoFilterBackend #lists, creates and filters tasks class ListTask(generics.ListCreateAPIView): queryset = models.Task.objects.all() serializer_class = TaskSerializer filter_backends = [DjangoFilterBackend, filters.SearchFilter] filterset_fields = ['date'] search_fields = ['description'] #allows to see detail of a task class DetailTask(generics.RetrieveUpdateDestroyAPIView): queryset = models.Task.objects.all() serializer_class = TaskSerializer
This is my test for the view (only for the first part since I have barely begun and I’m already running into many errors):
from todo.views import ListTask, DetailTask from todo.models import Task from rest_framework import status from django.test import TestCase from django.urls import reverse from rest_framework.response import Response from rest_framework.test import APIRequestFactory class ListTaskViewTest(TestCase): def setUp(self): Task.objects.create(title='title', description='description', completed=False) def test_delete_task(self): task = Task.objects.get(id=1) delete_task = reverse('tasks-api:destroy', task) request = self.factory.delete(delete_task) response = TaskDestroyAPIView.as_view()(request, task) self.assertEqual(response.status_code, 401)
This is the error log:
System check identified no issues (0 silenced). .....E ====================================================================== ERROR: test_delete_task (todo.tests.test_views.ListTaskViewTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\urls\resolvers.py", line 591, in url_patterns iter(patterns) TypeError: 'Task' object is not iterable The included URLconf 'title' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Any help as to how to write a proper test case for my view would be greatly appreciated!