lundi 24 septembre 2018

Url Django testing

urls.py of my web application is as follows

urlpatterns = [
    path('u/<int:unit_id>/', views.unit_view, name='unit_view'),
    path('u/<int:unit_id>/edit/', views.unit_edit, name='unit_edit'),
]

I triedn to write tests for urls are as following

from django.urls import reverse, resolve
from django.test import TestCase


class TestUrls(TestCase):

    def test_unit_view_url(self):
        path = reverse('unit_view', kwargs={'unit_id': 1})
        self.assertEqual('unit_view', resolve(path).views.unit_view)

    def test_unit_edit_url(self):
        path = reverse('unit_edit', kwargs={'unit_id': 1})
        assert resolve(path).views.unit_edit == 'unit_edit'

Getting error as

ERROR: test_unit_view_url (decentmark.tests.test_urls.TestUrls)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Prabhanjan\Documents\GitHub\decentmark\decentmark\tests\test_urls.py", line 8, in test_unit_view_url
    path = reverse('unit_view', kwargs={'unit_id': 1})
  File "C:\Users\Prabhanjan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\base.py", line 90, in r
everse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Users\Prabhanjan\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 622
, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'unit_view' not found. 'unit_view' is not a valid view function or pa
ttern name.

Aucun commentaire:

Enregistrer un commentaire