Skip to content

Instantly share code, notes, and snippets.

@kognate
Last active January 9, 2018 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kognate/ec3efec9b923b1714a05c3a2d151636c to your computer and use it in GitHub Desktop.
Save kognate/ec3efec9b923b1714a05c3a2d151636c to your computer and use it in GitHub Desktop.
from unittest import TestCase
from unittest.mock import patch
from email.mime.text import MIMEText
import os
import smtplib
def send_email(email_from, email_to, email_subject, email_msg):
with smtplib.SMTP(os.getenv('SMTP_SERVER','localhost')) as smtpObj:
to_msg = MIMEText(email_msg)
to_msg['Subject'] = email_subject
to_msg['From'] = email_from
to_msg['To'] = email_to
smtpObj.sendmail(email_from, email_to.split(','), to_msg.as_string())
class TestFunction(TestCase):
@patch.object(smtplib.SMTP, 'sendmail')
@patch.object(smtplib.SMTP, 'close')
def test_send_email(self, mock_close, mock_sendmail):
test_message = MIMEText('hello')
test_message['Subject'] = 'TEST'
test_message['From'] = 'testuser@test.com'
test_message['To'] = 'testuser@test.com,testuser2@test.com'
send_email(test_message['From'],
test_message['TO'],
test_message['Subject'],
'hello')
mock_close.assert_called()
mock_sendmail.assert_called_with(test_message['From'],
test_message['To'].split(','),
test_message.as_string())
if __name__ == '__main__':
TestFunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment