Proper way to test Django signals

Simplest way to do what you asked in 2015:

from unittest.mock import patch

def test_question_posted_signal_triggered(self, mock):
    form = YourForm()
    form.cleaned_data = {'name': 'Jan Nowak'}

    # Check that your signal was called.

    # Check that your signal was called only once.
    self.assertEqual(mock.call_count, 1)

    # Do whatever else, like actually checking if your signal logic did well.

And with that, you just tested that your signal was properly triggered.

Leave a Comment