首页 > 解决方案 > 未使用的导入使我可以访问信号的标准输出

问题描述

我刚刚遇到了一次奇怪的互动,我很好奇你们中的一个人能否解释发生了什么:

我正在编写一个测试,首先我创建了一个简单的打印语句来检查信号是否消失。这是位于的测试notifications/tests/test_notifications.py

def test_like_post_save_signal(self):
    """ Testing the signal that is connected to the Like model's post_save. """
    baker.make('core.Like')

这是我要测试的信号代码notifcations/notifications.py

@receiver(post_save, sender='core.Like')
def like_signal_post_save(sender, **kwargs):
    """ Post save signal for Like model """
    print("Something")

幕后发生了一些神奇的事情,但这里的目标是print("Something")每次保存 Like 对象时。

...

现在,这就是让我感到困惑的地方。我能够让它工作,但只有在从notifications.py我的test_notifications.py文件中导入一个单独的函数之后。

这是导入语句:from core.services.notifications.notifications import print_test

为什么不同函数 ( print_test) 的导入语句允许我访问标准输出?

标签: pythondjangopython-unittestdjango-signalsdjango-unittest

解决方案


推荐阅读