Testing with Fixtures
A snippet showing how to use fixtures in Django tests.
# models.py
from django.db import models
class Tag(models.Model):
name = models.CharField(max_length=50, unique=True)
# admin.py
from django.contrib import admin
from .models import Tag
@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = ("name",)
# Result (tests/test_fixtures.py)
from django.test import TestCase
from .models import Tag
class TagFixtureTest(TestCase):
fixtures = ["tags.json"]
def test_loaded(self):
self.assertTrue(Tag.objects.filter(name="news").exists())
Explanation:
-
Place fixture files in app's
fixtures/directory and reference by filename. - Fixtures speed up repetitive setup across tests.
- Category Testing
- Total Views 1008
- Last Modified 29 March, 2026
- Tags #testing #fixtures #data #unittest
Previous snippet
Using setUp and tearDown
Next snippet