我想对python中的电子邮件发送功能执行单元测试。 它使用AWS ses通过数据块发送电子邮件。 我尝试在模拟库和moto之类的python库上进行了许多小时的探索,但无法获得任何示例或文档来显示模拟aws ses的方法。
下面是我的功能:
import boto3
import matplotlib.pyplot as plt
from email import encoders
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(from_email, to_emails, subject, body_html, attachments=[], cc=[], bcc=[]):
attachment_ready_html = []
img_id = 0
mime_images = []
# iterate over raw HTML
for l in body_html:
# For each image in the body_html, convert each to a base64 encoded inline image
if l.startswith("<img"):
image_data = l[len("<img src='data:image/png;base64,"):-2]
mime_img = MIMEImage(base64.standard_b64decode(image_data))
mime_img.add_header('Content-ID', '<img-%d>' % img_id)
attachment_ready_html.append("<center><img src='cid:img-%d'></center>" % img_id)
img_id += 1
mime_images.append(mime_img)
else:
attachment_ready_html.append(l)
print("Added {} images".format(img_id))
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ", ".join(to_emails)
body = MIMEText('\n'.join(attachment_ready_html), 'html')
for i in mime_images:
msg.attach(i)
msg.attach(body)
for raw_attachment in attachments:
attachment = MIMEApplication(open(fname, 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', filename=raw_attachment)
msg.attach(attachment)
ses = boto3.client('ses', region_name='us-west-2')
ses.send_raw_email(
Source=msg['FROM'],
Destinations=to_emails,
RawMessage={'Data': msg.as_string()})
print "Sending Email."
是否有任何样本单元测试示例可以用来对该功能进行单元测试?有链接吗? 关于如何解决它的任何建议。 谢谢