Skip to content

Instantly share code, notes, and snippets.

@Wilfred
Created December 24, 2013 16:17
Show Gist options
  • Select an option

  • Save Wilfred/8115246 to your computer and use it in GitHub Desktop.

Select an option

Save Wilfred/8115246 to your computer and use it in GitHub Desktop.
unit testing django emails
from django.test import TestCase
from django.test.utils import override_settings
from mock import patch
# If we're sending emails asynchronously with Sentry, make it
# synchronous during tests:
@override_settings(CELERY_ALWAYS_EAGER=True)
class EmailTestCase(TestCase):
def setUp(self):
super(EmailTestCase, self).setUp()
self.emails_sent = []
def fake_send_mail(subject, message, from_email, recipient_list,
*args, **kwargs):
self.emails_sent.append({
'subject': subject, 'message': message,
'recipient_list': recipient_list
})
self.email_patcher = patch("emails.tasks.django_send_mail", fake_send_mail)
self.email_patcher.start()
def tearDown(self):
self.email_patcher.stop()
def assertEmailSentTo(self, email_address):
for email in self.emails_sent:
if email_address in email['recipient_list']:
return
self.fail('No email was sent to %s. The emails that were sent were: %s'
% (email_address, self.emails_sent))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment