Skip to content

Instantly share code, notes, and snippets.

@qcoumes
Last active April 17, 2024 15:56
Show Gist options
  • Save qcoumes/af9dd4b00297e9d4d4024aaba72f7001 to your computer and use it in GitHub Desktop.
Save qcoumes/af9dd4b00297e9d4d4024aaba72f7001 to your computer and use it in GitHub Desktop.
This fixture disable django's signals in every test. You can indiviually enable them for a function using '@pytest.mark.enable_signals' decorator or for a file using 'pytestmark = pytest.mark.enable_signals'
@pytest.fixture(autouse=True)
def mute_signals(request):
# Skip applying, if marked with `enabled_signals`
if 'enable_signals' in request.keywords:
return
signals = [pre_save, post_save, pre_delete, post_delete, m2m_changed]
restore = {}
for signal in signals:
restore[signal] = signal.receivers
signal.receivers = []
def restore_signals():
for signal, receivers in restore.items():
signal.sender_receivers_cache.clear()
signal.receivers = receivers
request.addfinalizer(restore_signals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment