Django 1.3 : Outbox empty during tests

标签 django django-testing

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

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

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.', 
                  'from@example.com', 
                  ['to@example.com'], 
                  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>]

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

最佳答案

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

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

关于Django 1.3 : Outbox empty during tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5424498/

相关文章:

python - Django Q 对象和 m2m 查询

django - 在 Django test.py 文件中进行每次测试后,如何重新评估我的 urls.py 文件?

django - 在 Django 模型中测试 'class Meta'

django - 带有 django 的 Pycharm 抛出 ImportError : cannot import name 'unittest'

django - 在谷歌上索引一个 heroku 网站

python - Django:上传的文件是二进制的。能改成utf吗?所以 readline() 返回 unicode 而不是字节

python - 如何注销 Django 中的用户?

python - Django 错误 TypeError at/admin/'set' 对象不可逆

python - django 测试 - 模型实例未创建

Django 的 self.client.login(...) 在单元测试中不起作用