test-api-serializer
Provides standard testing patterns for validating Django serializer logic and JSON outputs.
Install
mkdir -p .claude/skills/test-api-serializer && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/15123" && unzip -o skill.zip -d .claude/skills/test-api-serializer && rm skill.zipInstalls to .claude/skills/test-api-serializer
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Serializer tests: to_representation, to_internal_value, validation. Use when: testing DRF serializer output, input validation, custom field logic.Key capabilities
- →Test serializer output against expected JSON structure
- →Verify serializer validation rejects invalid input
- →Test custom `to_representation` methods
- →Test custom `to_internal_value` methods
- →Check nested serializer behavior
- →Test read-only field handling
How it works
The skill provides Python pytest examples for testing DRF serializers by asserting expected data structures, validation outcomes, and custom method behaviors.
Inputs & outputs
When to use test-api-serializer
- →Testing DRF serializer JSON output
- →Verifying serializer input validation
- →Checking custom serializer field logic
About this skill
API Serializer Tests
When to Use
- Testing serializer output matches expected JSON structure
- Verifying serializer validation rejects bad input
- Testing custom
to_representation/to_internal_value - Checking nested serializer behavior
Rules
Testing Serialization Output
import pytest
@pytest.mark.django_db
def test_serializer_output():
from tests.factories import FirmwareFactory
from apps.firmwares.serializers import FirmwareSerializer
fw = FirmwareFactory(version="1.0.0")
serializer = FirmwareSerializer(fw)
data = serializer.data
assert data["version"] == "1.0.0"
assert "id" in data
assert "created_at" in data
@pytest.mark.django_db
def test_serializer_excludes_sensitive():
from tests.factories import UserFactory
from apps.users.serializers import UserSerializer
user = UserFactory()
data = UserSerializer(user).data
assert "password" not in data
assert "email" in data
Testing Deserialization (Input Validation)
@pytest.mark.django_db
def test_serializer_valid_input():
from apps.firmwares.serializers import FirmwareSerializer
data = {"version": "1.0.0", "build_number": "100"}
serializer = FirmwareSerializer(data=data)
assert serializer.is_valid(), serializer.errors
@pytest.mark.django_db
def test_serializer_invalid_input():
from apps.firmwares.serializers import FirmwareSerializer
serializer = FirmwareSerializer(data={})
assert not serializer.is_valid()
assert "version" in serializer.errors
@pytest.mark.django_db
@pytest.mark.parametrize("field,value", [
("version", ""),
("version", None),
("file_size", -1),
])
def test_serializer_field_validation(field, value):
from apps.firmwares.serializers import FirmwareSerializer
serializer = FirmwareSerializer(data={field: value})
assert not serializer.is_valid()
assert field in serializer.errors
Testing Custom Validation
@pytest.mark.django_db
def test_serializer_validate_method():
from apps.firmwares.serializers import FirmwareSerializer
data = {"version": "1.0.0", "min_android": "15", "max_android": "10"}
serializer = FirmwareSerializer(data=data)
assert not serializer.is_valid()
assert "non_field_errors" in serializer.errors
Testing Read-Only Fields
@pytest.mark.django_db
def test_read_only_field_ignored():
from apps.firmwares.serializers import FirmwareSerializer
data = {"version": "1.0.0", "download_count": 999}
serializer = FirmwareSerializer(data=data)
if serializer.is_valid():
obj = serializer.save()
assert obj.download_count != 999 # Read-only, should be default
Testing Nested Serializers
@pytest.mark.django_db
def test_nested_serializer():
from tests.factories import FirmwareFactory
from apps.firmwares.serializers import FirmwareDetailSerializer
fw = FirmwareFactory()
data = FirmwareDetailSerializer(fw).data
assert "model" in data
assert "brand" in data["model"]
Red Flags
- Not testing both serialization and deserialization directions
- Missing validation tests for required fields
- Testing serializer through views instead of directly — harder to debug
- Not checking
serializer.errorscontent — just checkingis_valid()is weak
Quality Gate
& .\.venv\Scripts\python.exe -m ruff check . --fix
& .\.venv\Scripts\python.exe -m ruff format .
& .\.venv\Scripts\python.exe manage.py check --settings=app.settings_dev
When not to use it
- →When testing serializer through views
- →When not checking `serializer.errors` content
- →When not testing both serialization and deserialization directions
Limitations
- →The skill focuses on Django REST Framework serializers
- →The skill does not cover testing through views
- →The skill does not provide examples for all possible serializer scenarios
How it compares
This skill offers specific pytest patterns for DRF serializer testing, focusing on direct serializer interaction rather than testing through views, which can be harder to debug.
Compared to similar skills
test-api-serializer side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| test-api-serializer (this skill) | 0 | 4mo | No flags | Intermediate |
| fastapi-templates | 520 | 2mo | No flags | Intermediate |
| fastapi-pro | 79 | 4mo | No flags | Advanced |
| api-test-generator | 1 | 9mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by engremran07
View all by engremran07 →You might also like
fastapi-templates
wshobson
Create production-ready FastAPI projects with async patterns, dependency injection, and comprehensive error handling. Use when building new FastAPI applications or setting up backend API projects.
fastapi-pro
sickn33
Build high-performance async APIs with FastAPI, SQLAlchemy 2.0, and Pydantic V2. Master microservices, WebSockets, and modern Python async patterns. Use PROACTIVELY for FastAPI development, async optimization, or API architecture.
api-test-generator
mikopbx
Генерация полных Python pytest тестов для REST API эндпоинтов с валидацией схемы. Использовать при создании тестов для новых эндпоинтов, добавлении покрытия для CRUD операций или валидации соответствия API с OpenAPI схемами.
add-vault-protocol
tradingstrategy-ai
Add support for a new ERC-4626 vault protocol. Use when the user wants to integrate a new vault protocol like IPOR, Plutus, Morpho, etc. Requires vault smart contract address, protocol name, and protocol slug as inputs.
supabase-python
alinaqi
FastAPI with Supabase and SQLAlchemy/SQLModel
pagination
dadbodgeoff
Implement cursor-based and offset pagination for APIs. Covers efficient database queries, stable sorting, and pagination metadata.