18.6. Tests Urls
18.6.1. Setup
from django.test import SimpleTestCase
from django.urls import reverse, resolve
from shop.views import product_list, product_detail, ProductCreateView
18.6.2. Function Based Views
class TestUrls(SimpleTestCase):
def test_url_product_list(self):
url = reverse('product-list')
self.assertEqual(resolve(url).func, project_list)
18.6.3. Class Based Views
class TestUrls(SimpleTestCase):
def test_url_product_add(self):
url = reverse('product-add')
self.assertEqual(resolve(url).func.view_class, ProjectCreateView)
18.6.4. Parametrized Views
class TestUrls(SimpleTestCase):
def test_url_product_url_resolves(self):
url = reverse('product-detail', kwargs={'name': 'alpha'})
self.assertEqual(resolve(url).func, project_detail)
18.6.5. Example
File myproject/shop/tests/test_urls.py
:
from django.test import SimpleTestCase
from django.urls import reverse, resolve
from shop.views import product_list, product_detail, ProductCreateView
class TestUrls(SimpleTestCase):
def test_product_list(self):
url = reverse('shop:product-list')
self.assertEqual(resolve(url).func, product_list)
def test_product_create(self):
url = reverse('shop:product-create')
self.assertEqual(resolve(url).func.view_class, ProductCreateView)
def test_product_detail(self):
url = reverse('shop:product-detail', kwargs={'name': 'alpha'})
self.assertEqual(resolve(url).func, product_detail)