In test cases (unit testing), the Django pre_save signal cannot be caught

In Django, my pre_save signal capture code works well. However, in the test.py test files, the signal handler cannot receive anything. Is there a hint of this problem?

  • It seems that my test files and the signal handler are in different applications. Is this the cause of the problem?
+4
source share
1 answer

It seems that my test files and the signal handler are in different applications. Is this the cause of the problem?

Yes. Each tests.py application is atomic. import your signal registration code or manually pair them somewhere in your test to make sure they are listening:

You can put signal processing and registration code anywhere. However, you need to make sure that the module that it imports early so that the signal is processed before any signals are sent.

(From: Listening to signals, connecting receiver functions .)

+5
source

All Articles