编程语言
首页 > 编程语言> > python – 在测试用例(单元测试)中,无法捕获Django pre_save信号

python – 在测试用例(单元测试)中,无法捕获Django pre_save信号

作者:互联网

在Django中,我捕获pre_save信号的代码效果很好.但是,在tests.py中的测试用例中,信号处理程序无法接收任何内容.这个问题有什么暗示吗?

>我的测试用例和信号处理程序似乎在不同的应用程序中.这是问题的原因吗?

解决方法:

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

是.每个应用程序的tests.py都是原子的.导入您的信号注册码或在测试中的某个地方手动连接它们以确保它们正在收听:

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.

(来自:Listening to signals, Connecting receiver functions.)

标签:python,unit-testing,django,django-models,django-signals
来源: https://codeday.me/bug/20190621/1254451.html