Testing Signals
A snippet showing how to test Django signals.
# models.py
from django.db import models
class Note(models.Model):
text = models.CharField(max_length=100)
# admin.py
from django.contrib import admin
from .models import Note
@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
list_display = ("text",)
# Result (tests/test_signals.py)
from django.test import TestCase
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Note
triggered = {"count": 0}
@receiver(post_save, sender=Note)
def _count_saves(sender, **kwargs):
triggered["count"] += 1
Explanation:
- Connect a test-only receiver and assert side effects or counters.
- Disconnect in tearDown if you attach global receivers in tests.
- Category Testing
- Total Views 984
- Last Modified 18 March, 2026
- Tags #testing #signals #models #unittest
Previous snippet
Testing Templates Used in Views
Next snippet