在测试用例(单元测试)中,Django pre_save信号不能被捕获 [英] In test cases(unit-testing), Django pre_save signal can not be caught
问题描述
- 似乎我的测试用例和信号处理程序在不同的应用程序。这是否是问题的原因?
我的测试用例和信号处理程序在不同的应用程序。这是否是问题的原因?
是的。每个应用程序的 tests.py
是原子的。 导入
您的信号注册码或手动连接到您测试的某个地方,以确保他们正在收听:
您可以将信号处理和
注册码放在任何你喜欢的地方。
但是,您需要确保
的早期
导入的模块,以便在任何信号需要
之前,信号处理获得
注册发送。
(From: 收听信号,连接接收机功能 。
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?
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.)
这篇关于在测试用例(单元测试)中,Django pre_save信号不能被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!