Django 1.3:测试期间发件箱为空

发布于 2024-10-26 12:23:19 字数 1245 浏览 0 评论 0原文

也许我不明白发件箱是如何工作的,但从文档中我了解到它只是在测试期间捕获所有外发邮件。

我使用新应用程序创建了一个新项目,并添加了以下代码。

from django.test import TestCase
from django.core.mail import send_mail, outbox

class SimpleTest(TestCase):
    def test_basic_addition(self):
        send_mail('Subject here', 
                  'Here is the message.', 
                  '[email protected]', 
                  ['[email protected]'], 
                  fail_silently=False)

        self.assertEqual( len( outbox ), 1 )

当我运行 python manage.py test app_name 时,它​​给出了一个断言错误 0 != 1。我做错了什么吗?

更新

嗯,如果我导入 django.core.mail 并使用 mail.outbox 它确实有效,这很奇怪。

尝试比较 outbox 和 mail.outbox 的直接导入,它们都给出不同的结果

from django.core import mail
from django.core.mail import send_mail, outbox     
...
self.assertEqual(outbox, mail.outbox)

返回:

- []
+ [<django.core.mail.message.EmailMessage object at 0x1e1fd90>]

也许我已经工作了很长时间并且错过了一些真正明显的东西?

Maybe I don't understand how outbox works but from the documentation I understood that it just catches all outgoing mail during testing.

I created a new project with a new application and added the following code.

from django.test import TestCase
from django.core.mail import send_mail, outbox

class SimpleTest(TestCase):
    def test_basic_addition(self):
        send_mail('Subject here', 
                  'Here is the message.', 
                  '[email protected]', 
                  ['[email protected]'], 
                  fail_silently=False)

        self.assertEqual( len( outbox ), 1 )

When I run python manage.py test app_name it gives an assertion error that 0 != 1. Am I doing something wrong?

Update

Well this is weird if I import django.core.mail and use mail.outbox it does work.

Tried to compare the direct import of outbox and mail.outbox and they both give different results

from django.core import mail
from django.core.mail import send_mail, outbox     
...
self.assertEqual(outbox, mail.outbox)

returns:

- []
+ [<django.core.mail.message.EmailMessage object at 0x1e1fd90>]

Maybe I've been working to long and missing something really obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-11-02 12:23:19

也许我真的应该阅读文档。

发件箱属性是一个特殊属性,仅在使用 locmem 电子邮件后端时创建。它通常不作为 django.core.mail 模块的一部分存在,并且您无法直接导入它

https://docs.djangoproject.com/en/dev /主题/测试/工具/#email-services

Maybe I should actually read the documentation.

The outbox attribute is a special attribute that is created only when the locmem e-mail backend is used. It doesn't normally exist as part of the django.core.mail module and you can't import it directly.

https://docs.djangoproject.com/en/dev/topics/testing/tools/#email-services

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文