Testing API Views with DRF
A snippet showing how to test DRF API views with APIClient.
# models.py
from django.db import models
class Todo(models.Model):
title = models.CharField(max_length=100)
is_done = models.BooleanField(default=False)
# admin.py
from django.contrib import admin
from .models import Todo
@admin.register(Todo)
class TodoAdmin(admin.ModelAdmin):
list_display = ("title", "is_done")
# Result (tests/test_api.py)
from rest_framework.test import APITestCase, APIClient
from django.urls import reverse
class TodoApiTest(APITestCase):
def setUp(self):
self.client = APIClient()
def test_list(self):
res = self.client.get(reverse("todo-list"))
self.assertEqual(res.status_code, 200)
Explanation:
-
Use DRF's
APITestCase/APIClientfor API testing with auth helpers. - Assert status, JSON structure, and pagination.
- Category Testing
- Total Views 534
- Last Modified 01 April, 2026
- Tags #testing #drf #api #views
Previous snippet
Testing Admin Views
Next snippet