django send_mail not working in testcase

  1. Import necessary modules:
from django.core import mail
from django.test import TestCase
  1. Create a test case class that inherits from django.test.TestCase:
class YourTestCase(TestCase):
  1. Define a test method within the test case class:
def test_send_mail(self):
  1. Set up any necessary data or objects for the test:
subject = 'Test Subject'
message = 'Test Message'
from_email = '[email protected]'
recipient_list = ['[email protected]']
  1. Use the mail.outbox list to capture sent emails:
mail.send_mail(subject, message, from_email, recipient_list)
  1. Assert that the email was sent:
self.assertEqual(len(mail.outbox), 1)
  1. Access the sent email from the mail.outbox list:
sent_email = mail.outbox[0]
  1. Assert specific details of the sent email, such as subject, message, from_email, and recipient_list:
self.assertEqual(sent_email.subject, subject)
self.assertEqual(sent_email.body, message)
self.assertEqual(sent_email.from_email, from_email)
self.assertEqual(sent_email.recipients(), recipient_list)

Note: Ensure that your email backend is properly configured in your Django settings for testing, and that the EMAIL_BACKEND setting is set to 'django.core.mail.backends.locmem.EmailBackend' or another appropriate testing backend. Also, make sure your test case is correctly set up within your Django app and that it is being discovered and executed by your test runner.