Django 1.3:测试期间发件箱为空 [英] Django 1.3: Outbox empty during tests

查看:100
本文介绍了Django 1.3:测试期间发件箱为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

        self.assertEqual( len( outbox ), 1 )

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

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

更新

我导入django.core.mail并使用它做的mail.outbox工作。

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

试图比较直接导入发件箱和mail.outbox,他们都会提供不同的结果

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)

返回:

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

也许我一直在努力寻找一些很明显的东西?

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

推荐答案

阅读文档。


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

https:// docs.djangoproject.com/en/dev/topics/testing/overview/#django.core.mail.django.core.mail.outbox

这篇关于Django 1.3:测试期间发件箱为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆