Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Prevent receivers from running in test suites
import functools
from django.conf import settings
from django.dispatch import receiver
def suspendingreceiver(signal, **decorator_kwargs):
A wrapper around the standard django receiver that prevents the receiver
from running if the setting `SUSPEND_SIGNALS` is `True`.
This is particularly useful if you want to prevent signals running during
unit testing.
class MyTestCase(TestCase):
def test_method(self):
Model.objects.create() # post_save_receiver won't execute
@suspendingreceiver(post_save, sender=Model)
def post_save_receiver(sender, instance=None, **kwargs):
def our_wrapper(func):
@receiver(signal, **decorator_kwargs)
def fake_receiver(sender, **kwargs):
if settings.SUSPEND_SIGNALS:
return func(sender, **kwargs)
return fake_receiver
return our_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.