Reputation: 4461
I've just tried testing the sending of emails. Well, test pass whereas I have not assigned anything in settings. This was assumed to be TDD. The next step would be assigning of EMAIL_HOST, EMAIL_PORT, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_USE_TLS, DEFAULT_FROM_EMAIL, SERVER_EMAIL.
Well, the test pass. I can't catch how to test automatically whether the above mentioned settings are correct.
Below are my tests. Thank you in advance for a kick here.
from django.test import TestCase
from django.core import mail
from django.core.mail import send_mail
class GeneralTest(TestCase):
def test_send_email(self):
number_of_emails_sent = send_mail('Subject here',
'Here is the message.',
'[email protected]',
['[email protected]'],
fail_silently=False)
self.assertEqual(number_of_emails_sent, 1)
def test_send_email_1(self):
mail.send_mail(
'Subject here', 'Here is the message.',
'[email protected]', ['[email protected]'],
fail_silently=False,
)
pdb.set_trace()
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].subject, 'Subject here')
Upvotes: 2
Views: 652
Reputation: 19615
Django's test runner automatically redirects emails sent during tests to a fake email backend.
If any of your Django views send email using Django’s email functionality, you probably don’t want to send email each time you run a test using that view. For this reason, Django’s test runner automatically redirects all Django-sent email to a dummy outbox. This lets you test every aspect of sending email – from the number of messages sent to the contents of each message – without actually sending the messages.
You can use the mail.outbox
variable to check that the messages you would otherwise have sent are correct.
In your example, you could check that mail.outbox
contains a single EmailMessage with a subject of 'Subject here', but it would make more sense to run the part of your application that email instead, and test that.
If you want to send an actual email as part of your test, you can pass a real backend in the connection
argument to send_mail
.
Upvotes: 2