简体   繁体   中英

How to fix this error failed to import test module while testing?

Here I'm writing some TestCase for some queryset to view in api and getting error not a valid function or pattern name. I didn't get any idea what missing here? Is there any solution for this?

views.py

class  StudentView(generics.ListAPIView):
queryset = StudentDetails.objects.raw('SELECT * FROM 
           collegedetails.college_studentdetails LIMIT 3;')
serializer_class = StudentDetailsSerializers

test_views.py

from rest_framework.test import APITestCase
from rest_framework.reverse import reverse
from rest_framework import status

STUDENT_URL = reverse('student/')
class StudentsDetailsTest(APITestCase):
def test_details(self):
    response = self.client.get(STUDENT_URL, format='json')
    self.assertEqual(response.status_code, status.HTTP_200_OK)

college/urls.py

urlpatterns=[

        path('student/',views.StudentView.as_view(), name='student'),
]

traceback error

Found 1 test(s).
System check identified no issues (0 silenced).
E
======================================================================
ERROR: college.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: college.tests
Traceback (most recent call last):
  File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "C:\Users\\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "C:\Users\\collegedjango\MYSITE\college\tests.py", line 33, in 
<module>
    STUDENT_URL = reverse('student')
  File "C:\Users\\collegedjango\venv\lib\site- 
packages\rest_framework\reverse.py", line 47, in reverse
    url = _reverse(viewname, args, kwargs, request, format, **extra)
  File "C:\Users\\collegedjango\venv\lib\site- 
packages\rest_framework\reverse.py", line 60, in _reverse
    url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
  File "C:\Users\\collegedjango\venv\lib\site- 
packages\django\urls\base.py", line 86, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\\collegedjango\venv\lib\site- 
packages\django\urls\resolvers.py", line 729, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'student' not found. 
 'student' is not a valid view function or pattern name.


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

You used an app_name = 'student_api' , this means that you should prefix the name of the view with that app_name , so:

STUDENT_URL = reverse('student_api:student')

You should furthermore remove the slash at the end: the reverse uses the name of the view (prefixed with the app_name or namespace if you defined one), not the path pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM