При запуске модульных тестов электронные письма сохраняются в виде EmailMessage объектов в списке в django.core.mail.outbox
, после чего вы можете выполнять любые проверки в своем классе тестирования.Ниже приведен пример из документов django .
from django.core import mail
from django.test import TestCase
class EmailTest(TestCase):
def test_send_email(self):
# Send message.
mail.send_mail('Subject here', 'Here is the message.',
'from@example.com', ['to@example.com'],
fail_silently=False)
# Test that one message has been sent.
self.assertEqual(len(mail.outbox), 1)
# Verify that the subject of the first message is correct.
self.assertEqual(mail.outbox[0].subject, 'Subject here')
В качестве альтернативы, если вы просто хотите визуально проверить содержимое письма во время разработки, вы можете установить EMAIL_BACKEND
to:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
, а затем посмотрите на консоль.
или
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
, затем проверьте файл.