我正在为一个使用django-post_office包来实现其大部分电子邮件功能的应用程序编写测试。
默认django.core.mail
库包含大量有用的工具,用于测试是否真的有电子邮件正在发送。(在测试期间没有实际发送任何内容)
class TestFunctionThatSendsEmail(Testcase):
@override_settings(
EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend'
)
def test_function_sends_email(self):
self.assertEqual(len(outbox), 0)
run_function_that_calls_email()
self.assertEqual(len(outbox), 1)
...
# other tests
但是,我们函数中的电子邮件是通过django-post_office
mail.send()
函数发送的
# priority now to make sure that they are being sent right away
mail.send(
sender=sender,
recipients=to,
context=context,
template=template_name,
priority='now',
)
这会导致上面的测试失败,因为由于某种原因,电子邮件不会最终出现在发件箱中。
奇怪的是,如果我将电子邮件更改为EMAIL_BACKEND
确实django.core.mail.backends.console.EmailBackend
显示在我的终端中,那么它正在监听EMAIL_BACKEND
设置。
我尝试在django-post_office
github 中寻找替代方法/功能来测试此功能,但我能找到的只是检查电子邮件是否保存到数据库并验证其状态的建议。(我做了并且工作)但是 django 似乎无法检测到任何实际发送的电子邮件的事实让我有点紧张。
有没有人知道一种方法可以使发送的电子邮件django-post_office
出现在发件箱中,或者,如果不可能,一种方法来确保它们确实被发送?(除了检查数据库)