5

In Django, my code on catching pre_save signal works well. However, in testcases in tests.py, the signal handler cannot receive anything. Is there any hint for this problem?

  • It seems that my testcases and signal handler are in different apps. Is this the cause of the problem?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wei An
  • 1,779
  • 4
  • 13
  • 18

1 Answers1

5

It seems that my testcases and signal handler are in different apps. Is this the cause of the problem?

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

You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Udi
  • 29,222
  • 9
  • 96
  • 129