Testing EMail sending in Django =============================== class TestOnboardingEmails(TestCase): MSG1 = 'Congratulations! You have now set your first ' 'assignment' MSG2 = 'You have been using product for a week now' def setUp(self): self.teacher = factories.TeacherFactory.create() self.teacher.email = 'test@test.domani.com' self.teacher.save() self.student = factories.StudentFactory.create() self.studentset = factories.StudentSetFactory.create(owner=self.teacher) self.studentset.join_student(self.student) self.studentset.join_teacher(self.teacher) self.quick = factories.QuickFactory.create() self.original_backend = settings.EMAIL_BACKEND settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' def tearDown(self): settings.EMAIL_BACKEND = self.original_backend def test_new_assignment(self): aset = AssignmentSet.create(teacher=self.teacher, assign_date=datetime.now(), studentset=self.studentset, students_list=[self.student], content_list=[{'obj': self.quick, 'attempt_max':1}]) cmd = process_events.Command() cmd.handle(no_celery=True, debug=False, name=None, date=None, quiet=False) self.assertTrue(len(mail.outbox)==1) self.assertTrue(self.MSG1 in mail.outbox[0].message().as_string()) self.assertTrue(self.MSG2 not in mail.outbox[0].message().as_string()) after_1_week_plus = datetime.now() + relativedelta(days=8) cmd.handle(no_celery=True, debug=False, name=None, date=after_1_week_plus.strftime(DATE_FORMAT), quiet=False) self.assertTrue(len(mail.outbox)==2) self.assertTrue(self.MSG1 not in mail.outbox[1].message().as_string()) self.assertTrue(self.MSG2 in mail.outbox[1].message().as_string()) if __name__ == '__main__': unittest.main() The important lines in setUp and tearDown: def setUp(self): self.original_backend = settings.EMAIL_BACKEND settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' def tearDown(self): settings.EMAIL_BACKEND = self.original_backend