unittest发送测试报告邮件
About回到顶部
有了测试报告我们就可以发送邮件了。
Python发邮件功能借助smtplib
和email
模块。
更多关于发邮件的故事点击:https://www.cnblogs.com/Neeo/articles/11199127.html
代码示例回到顶部
import unittest
import smtplib
import HTMLTestRunner
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
class TestStringMethods(unittest.TestCase):
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_upper</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">"""判断 foo.upper() 是否等于 FOO"""</span>
self.assertEqual(<span class="hljs-string">'foo'</span>.upper(), <span class="hljs-string">'FOO'</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_isupper</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-string">""" 判断 Foo 是否为大写形式 """</span>
self.assertTrue(<span class="hljs-string">'Foo'</span>.isupper())
def get_case_result():
""" 获取测试用例报告 """
suite = unittest.makeSuite(TestStringMethods)
file_path = r'M: ests
esult.html'
with open(file_path, 'wb') as f:
HTMLTestRunner.HTMLTestRunner(
stream=f,
title='HTMLTestRunner版本关于upper的测试报告',
description='判断upper的测试用例执行情况').run(suite)
f1 = open(file_path, 'r', encoding='utf-8')
res = f1.read()
f1.close()
return res
def send_email():
""" 发送邮件 """
<span class="hljs-comment"># 第三方 SMTP 服务</span>
mail_host = <span class="hljs-string">"smtp.qq.com"</span> <span class="hljs-comment"># 设置服务器</span>
mail_user = <span class="hljs-string">"1206180814@qq.com"</span> <span class="hljs-comment"># 用户名</span>
mail_pass = <span class="hljs-string">"chmbpeciazgjgegi"</span> <span class="hljs-comment"># 口令</span>
<span class="hljs-comment"># 设置收件人和发件人</span>
sender = <span class="hljs-string">'1206180814@qq.com'</span>
receivers = [<span class="hljs-string">'1206180814@qq.com'</span>, <span class="hljs-string">'tingyuweilou@163.com'</span>] <span class="hljs-comment"># 接收邮件,可设置为你的QQ邮箱或者其他邮箱</span>
<span class="hljs-comment"># 创建一个带附件的实例对象</span>
message = MIMEMultipart()
<span class="hljs-comment"># 邮件主题、收件人、发件人</span>
subject = <span class="hljs-string">'请查阅--测试报告'</span> <span class="hljs-comment"># 邮件主题</span>
message[<span class="hljs-string">'Subject'</span>] = Header(subject, <span class="hljs-string">'utf-8'</span>)
message[<span class="hljs-string">'From'</span>] = Header(<span class="hljs-string">"{}"</span>.format(sender), <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># 发件人</span>
message[<span class="hljs-string">'To'</span>] = Header(<span class="hljs-string">"{}"</span>.format(<span class="hljs-string">';'</span>.join(receivers)), <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># 收件人</span>
<span class="hljs-comment"># 邮件正文内容 html 形式邮件</span>
send_content = get_case_result() <span class="hljs-comment"># 获取测试报告</span>
html = MIMEText(_text=send_content, _subtype=<span class="hljs-string">'html'</span>, _charset=<span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># 第一个参数为邮件内容</span>
<span class="hljs-comment"># 构造附件</span>
att = MIMEText(_text=send_content, _subtype=<span class="hljs-string">'base64'</span>, _charset=<span class="hljs-string">'utf-8'</span>)
att[<span class="hljs-string">"Content-Type"</span>] = <span class="hljs-string">'application/octet-stream'</span>
file_name = <span class="hljs-string">'result.html'</span>
att[<span class="hljs-string">"Content-Disposition"</span>] = <span class="hljs-string">'attachment; filename="{}"'</span>.format(file_name) <span class="hljs-comment"># # filename 为邮件附件中显示什么名字</span>
message.attach(html)
message.attach(att)
<span class="hljs-keyword">try</span>:
smtp_obj = smtplib.SMTP()
smtp_obj.connect(mail_host, <span class="hljs-number">25</span>) <span class="hljs-comment"># 25 为 SMTP 端口号</span>
smtp_obj.login(mail_user, mail_pass)
smtp_obj.sendmail(sender, receivers, message.as_string())
smtp_obj.quit()
print(<span class="hljs-string">"邮件发送成功"</span>)
<span class="hljs-keyword">except</span> smtplib.SMTPException:
print(<span class="hljs-string">"Error: 无法发送邮件"</span>)
if name == 'main':
send_email()
发送效果如下图:
正如上面两张图片所示,在使用HTMLTestRunner模板发送测试报告的时候,QQ邮箱和163邮箱或多或少的会存在一些渲染问题。的html内容无法加载CSS样式,但附件没问题。
这说明各家对于邮箱服务器的设置不一样。我们不要在意这些细节,只要附件没有问题就ok啦。