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

标签 python django unit-testing django-models django-signals

在 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 - 在测试用例(单元测试)中,无法捕获 Django pre_save 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6281168/

相关文章:

django - Django-haystack 使用 Solr 索引列表而不是原始数据

java - 使用 Spock 模拟 Java 中的私有(private)静态最终变量

laravel - 如何测试监听器发送的 Laravel 通知?

c++ - 如何在编译时排除单元测试

python - 使用日期范围的 pandas 的滚动加权平均值

python - 获取一个Python项目中所有未使用的变量

python - 在我的包的子包中运行 python 脚本

python - Django 的扩展和库

python - Django 管理员并显示缩略图

sql - 慢速 SQL 事务从 Postgres 中的表中获取最新的时间戳行