TE

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.zip

Installs 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.
146 chars✓ has a “when” trigger
Intermediate

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

You give it
Django REST Framework serializer and data
You get back
Pytest assertions verifying serializer behavior

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.errors content — just checking is_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.

SkillInstallsUpdatedSafetyDifficulty
test-api-serializer (this skill)04moNo flagsIntermediate
fastapi-templates5202moNo flagsIntermediate
fastapi-pro794moNo flagsAdvanced
api-test-generator19moReviewIntermediate

Try saying

Example prompts that trigger this skill in your AI assistant.

Search skills

Search the agent skills registry