Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading