An undocumented or untested API is an unusable API. In professional engineering teams, every REST endpoint must have automated unit tests running on CI/CD pipelines and interactive OpenAPI 3.0 / Swagger UI documentation.
# blog/tests.py
from django.contrib.auth import get_user_model
from rest_framework.test import APITestCase
from rest_framework import status
from django.urls import reverse
from .models import BlogPost, Category
User = get_user_model()
class BlogPostAPITests(APITestCase):
def setUp(self):
# Create test user and category
self.user = User.objects.create_user(email='test@example.com', password='password123')
self.category = Category.objects.create(name='Tech', slug='tech')
self.post = BlogPost.objects.create(
title='Test Article',
slug='test-article',
content='Test content body.',
category=self.category,
is_published=True
)
def test_get_posts_list_unauthenticated(self):
"""Ensure public visitors can fetch published posts list."""
url = reverse('post-list') # DefaultRouter pattern
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data['results']), 1)
def test_create_post_requires_authentication(self):
"""Ensure unauthenticated users cannot create posts (401 Unauthorized)."""
url = reverse('post-list')
data = {'title': 'New Post', 'content': 'Body'}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_create_post_authenticated(self):
"""Ensure authenticated users can create posts."""
self.client.force_authenticate(user=self.user)
url = reverse('post-list')
data = {
'title': 'Authenticated Post',
'content': 'Valid article content body.',
'category_id': self.category.id
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(BlogPost.objects.count(), 2)
# Install drf-spectacular
pip install drf-spectacular
# mywebsite/settings.py
INSTALLED_APPS += ['drf_spectacular']
REST_FRAMEWORK['DEFAULT_SCHEMA_CLASS'] = 'drf_spectacular.openapi.AutoSchema'
SPECTACULAR_SETTINGS = {
'TITLE': 'Veewom Public API',
'DESCRIPTION': 'Official RESTful API documentation for Veewom learning platform.',
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
}
Expose interactive Swagger and Redoc documentation in urls.py:
# mywebsite/urls.py
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
urlpatterns = [
# Schema download endpoint
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
# Interactive Swagger UI:
path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
# Clean Redoc UI:
path('api/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
]
Broaden your backend architecture, REST API design, and production engineering expertise:
Review DRF core setup and browsable API features.
Ensure all serializer fields have docstrings for OpenAPI generators.
Serve interactive Swagger UI documentation in production environments.
Showcase tested, documented public APIs in your developer portfolio.