I'd like to write a Django unit test which tests that an email is sent without actually sending it (as described in https://docs.djangoproject.com/en/2.0/topics/testing/tools/#email-services), but can also be configured to send an actual email (to inspect its formatting, etc.).
Right now, I have just an 'ordinary' test:
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)
(The test makes use of factory_boy
test fixtures for certain models). This test passes, but if I comment in the override_settings
decorator, I get an error:
(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'...
Since the mail.outbox
is empty, I would expect that I would receive an actual email, but I checked my inbox and didn't receive anything.
For the sake of completeness, here is the function I'm testing:
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()
To increase my confusion, from the AnyMail docs (http://anymail.readthedocs.io/en/stable/tips/test_backend/#testing-your-app), it would seem that AnyMail also captures emails in the outbox
in a test.
Can someone point out might what be going wrong here? Why am I neither receiving an email, nor seeing anything in the mail.outbox
?