在Django单元测试驱动程序中,如何测试是否发送了电子邮件?

时间:2010-01-29 01:23:02

标签: django django-testing

在Django单元测试驱动程序中,如何测试是否发送了电子邮件?

2 个答案:

答案 0 :(得分:5)

我不是Django大师(温和地说)但看起来有一些关于测试电子邮件的文档:Testing Django Applications | E-mail services。请注意,演示的方法适用于Django 1.0和更新版本。

答案 1 :(得分:1)

以下是忘记密码api调用的一些代码

def test_forgot_password(self):
    """
        This test makes sure the forgot password api call is working ...
    """

    data = {
        'username' : self.user.email,
    }

    self.assertTrue(self.user.forgot_pw_hash is None)
    response = self.c.post(reverse('api_forgot_password'), data, HTTP_X_REQUESTED_WITH='XMLHttpRequest')

    # make sure there is an email in the out box and make sure
    # the subject is correct
    self.assertEquals(mail.outbox[0].subject,'Reset Password')
    self.assertTrue(User.objects.get(email=self.user.email).forgot_pw_hash is not None)