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() then resolve() to ensure the URL maps to the right view.
  • For class-based views, compare match.func.view_class.
  • Category Testing
  • Total Views 458
  • Last Modified 02 April, 2026
  • Tags #testing #urls #resolution #views
Previous snippet
Testing Forms Validation
Next snippet
Testing Redirects
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.