Last active
August 9, 2024 06:42
-
-
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'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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