Testing Admin Views

A snippet showing how to test Django admin views.


# models.py

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=80)
      

# admin.py

from django.contrib import admin
from .models import City

@admin.register(City)
class CityAdmin(admin.ModelAdmin):
    list_display = ("name",)
      

# tests/test_admin.py

from django.test import TestCase
from django.contrib.auth import get_user_model
from django.urls import reverse

class AdminViewsTest(TestCase):
    def setUp(self):
        User = get_user_model()
        self.user = User.objects.create_superuser("admin", "[email protected]", "pass")
        self.client.login(username="admin", password="pass")
    def test_changelist_loads(self):
        url = reverse("admin:index")
        res = self.client.get(url)
        self.assertEqual(res.status_code, 200)
      
Explanation:
  • Log in as a superuser to access Admin views in tests.
  • Verify changelist, add, and change pages render.
  • Category Testing
  • Total Views 675
  • Last Modified 15 March, 2026
  • Tags #testing #admin #views #client
Previous snippet
Testing Email Sending
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.