4

How can I disable a signal for all tests within a Django TestCase when I create data using fixtures?

The following approach adapted from this answer does not work unfortunately. I suppose fixtures are applied before the setUp stage.

from django.db.models import signals
from django.test import TestCase


class MyTestCase(TestCase):
    fixtures = ["some_data.json"]

    def setUp(self) -> None:
        signals.post_save.disconnect(sender=MyModel, dispatch_uid="some_signal_uid")

    def test_some_test(self):
        # do something


@receiver(post_save, sender=MyModel,dispatch_uid="some_signal_uid")
def some_signal(sender, instance: MyModel, created: bool, **kwargs):
    # do something
nbeuchat
  • 6,575
  • 5
  • 36
  • 50
  • 4
    [The docs](https://docs.djangoproject.com/en/3.0/topics/testing/tools/#fixture-loading) explain that the fixtures are loaded once for the class to speed up the tests. Try disconnecting in [`setUpClass`](https://github.com/django/django/blob/235b68135172c473e022045931719f047cf3dc4a/django/test/testcases.py#L1107) instead of `setUp`. – Alasdair Apr 30 '20 at 15:11
  • @Alasdair that worked indeed! – nbeuchat Apr 30 '20 at 16:43

0 Answers0