lundi 30 mars 2020

Test case written for url resolver using resolve() in django application not working

I am trying to write test cases for simple user login and registration system in django. First, I was thinking of writing test cases for the urls. The only test case I have written so far is

from django.test import SimpleTestCase
from django.urls import reverse, resolve, path
from main.views import homepage, register, login_request, logout_request
import json

# Create your tests here.

class TestUrls(SimpleTestCase):

      def test_list_is_resolved(self):
          url = reverse('homepage')
          self.assertEquals(resolve(url).func,homepage)

The default urls.py is

 from django.contrib import admin
 from django.urls import path, include

 urlpatterns = [
      path('tinymce/',include('tinymce.urls')),
      path("",include('main.urls')),
      path('admin/', admin.site.urls),
           ]`

The main application urls.py is

 from django.urls import path
 from . import views

 app_name='main' # here for namespacing the urls

 urlpatterns = [
      path("", views.login_request, name="login"),
      path("homepage/",views.homepage, name="homepage"),
      path("register/", views.register,name="register"),
      path("logout", views.logout_request, name="logout"),
        ]`

Now every time I am running the tests, I am getting the following error.

(myproject) C:\Users\rohan\mysite>py manage.py test
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_list_is_resolved (main.tests.test_urls.TestUrls)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\rohan\mysite\main\tests\test_urls.py", line 11, in test_list_is_resolved
    url = reverse('homepage')
  File "C:\Users\rohan\Envs\myproject\lib\site-packages\django\urls\base.py", line 87, in reverse
   return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\rohan\Envs\myproject\lib\site-packages\django\urls\resolvers.py", line 677, in 
_reverse_with_prefix
  raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'homepage' not found. 'homepage' is not a valid 
view 
function or pattern name.

----------------------------------------------------------------------
 Ran 1 test in 0.015s

FAILED (errors=1)

I am not being able to find any error. What is wrong here?

Aucun commentaire:

Enregistrer un commentaire