Testing URL Resolution
A snippet showing how to test Django URL resolutions.
# models.py
from django.db import models
class Dummy(models.Model):
name = models.CharField(max_length=20, default="x")
# admin.py
from django.contrib import admin
from .models import Dummy
@admin.register(Dummy)
class DummyAdmin(admin.ModelAdmin):
list_display = ("name",)
# Result (tests/test_urls.py)
from django.test import SimpleTestCase
from django.urls import resolve, reverse
from .views import home
class UrlsTest(SimpleTestCase):
def test_home_url(self):
url = reverse("home")
match = resolve(url)
self.assertEqual(match.func, home)
Explanation:
-
Use
reverse()thenresolve()to ensure the URL maps to the right view. -
For class-based views, compare
match.func.view_class.
- Category Testing
- Total Views 459
- Last Modified 02 April, 2026
- Tags #testing #urls #resolution #views
Previous snippet
Testing Forms Validation
Next snippet