I have two Django apps, foo
and bar
, which are both listed in the INSTALLED_APPS
setting. foo
sends a Signal which is received by bar
. I want to test foo
and isolate it, so that bar
should not listen to the signal.
bar
connects to the signal using the AppConfig.ready()
method:
# bar/apps.py
class BarConfig(AppConfig):
def ready(self):
from foo.signals import foo_signal
from .signals import handle_foo_signal
foo_signal.connect(handle_foo_signal)
In the test code, I remove bar
from the INSTALLED_APPS
setting, using the @modify_settings()
decorator:
# foo/tests/test_foo.py
class TestFoo(TestCase):
@modify_settings(INSTALLED_APPS=dict(remove=["bar"]))
def test_foo(self):
...test code...
I expect, that bar
would not listen to the signal. However, it does.
I know, that I could disconnect each Signal manually. But I’m looking for a way to temporary “remove” an app entirely, including its signal handlers. Is there a way to isolate apps and their Signal receivers easily?
I debugged the code and found out, that the bar
app is still in INSTALLED_APPS
when ready()
is called.
I expected, that the app config is not called at all.
I use Django 3.2.16.