我想编写一个 Django 单元测试来测试是否发送了电子邮件而不实际发送它(如https://docs.djangoproject.com/en/2.0/topics/testing/tools/#email-services中所述) , 但也可以配置为发送实际的电子邮件(检查其格式等)。
现在,我只有一个“普通”测试:
from django.test import TestCase, override_settings
from django.core import mail
from lucy_web.test_factories import FamilyFactory, UserFactory
from lucy_web.views.app_invite import send_activation_email
# @override_settings(EMAIL_BACKEND="anymail.backends.mailgun.EmailBackend")
class SendActivationEmailTestCase(TestCase):
def test_send_activation_email(self):
user = UserFactory(email='kurt@startwithlucy.com')
family = FamilyFactory(employee_user=user)
send_activation_email(user=user)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].recipients(), [user.email])
self.assertIn(
f"Your activation code is {family.activation_code}.",
mail.outbox[0].body)
(该测试使用factory_boy
了某些型号的测试夹具)。这个测试通过了,但是如果我在override_settings
装饰器中评论,我会得到一个错误:
(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ python manage.py test lucy_web.tests.test_app_invite
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_send_activation_email (lucy_web.tests.test_app_invite.SendActivationEmailTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/kurtpeek/Documents/Dev/lucy2/lucy-web/lucy_web/tests/test_app_invite.py", line 15, in test_send_activation_email
self.assertEqual(len(mail.outbox), 1)
AssertionError: 0 != 1
----------------------------------------------------------------------
Ran 1 test in 0.787s
FAILED (failures=1)
Destroying test database for alias 'default'...
由于mail.outbox
是空的,我希望我会收到一封实际的电子邮件,但我检查了我的收件箱并没有收到任何东西。
为了完整起见,这是我正在测试的功能:
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
def send_activation_email(user):
context = {
'activation_code': user.family.activation_code,
'page_title': 'Lucy Invitation',
'company': user.family.package.company,
'num_sessions': user.family.session_count,
'domain': settings.EMAIL_IMAGE_DOMAIN}
message_text = render_to_string('email/activation_code.txt', context)
message_html = render_to_string('email/activation_code.html', context)
email = EmailMultiAlternatives(
subject='LUCY Invitation',
body=message_text,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[user.email],
reply_to=[settings.SUPPORT_EMAIL])
email.attach_alternative(message_html, "text/html")
email.send()
为了增加我的困惑,从 AnyMail 文档(http://anymail.readthedocs.io/en/stable/tips/test_backend/#testing-your-app)来看,AnyMail 似乎也在outbox
测试中捕获了电子邮件。
有人可以指出这里可能出了什么问题吗?为什么我既没有收到电子邮件,也没有看到邮件中的任何内容mail.outbox
?