金谷文具李世杰 發表於 2019-5-6 14:05:00

python接口自动化(二十九)--html测试报告通过邮件发出去——上(详解)

<h2>简介</h2>
<p>  前边几篇,已经教小伙伴们掌握了如何生成HTML的测试报告,那么生成测试报告,我们也不能放在那里不管了,这样即使你报告在漂亮,领导也看不到。因此如果想向领导汇报工作,不仅需要提供更直观的测试报告。而是我们需要将生</p>
<p>成测试报告发个相关的负责人,需要他们看一下测试结果,把控一下项目的接口有风险,会不会影响项目进度等等一些事吧。</p>
<p>  发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件。</p>
<h2>大致思路流程</h2>
<p>1、通过unittest框架的discover()找到匹配的测试用例,由HTMLTestRunner的run()方法执行测试用例并生成最新的测试报告。</p>
<p>2、调用new_report()函数找到测试报告目录下最新生成的测试报告,返回测试报告的路径。</p>
<p>3、将得到的最新测试报告的完整路径传给send_mail()函数,实现发邮件功能。</p>
<p>4、第一步,前边几篇,我们都详细介绍了,所以这篇就不在赘述了,直接从第二步、第三步开始讲解。</p>
<h2>查找最新测试报告&nbsp;</h2>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506110256734-588764790.png" alt=""></p>
<h2>定义发送邮件</h2>
<p>1、查看一下,源码如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506134115515-1276667820.png" alt=""></p>
<p>2、仿照源码进行改造</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506134252881-1879931191.png" alt=""></p>
<h2>参考代码</h2>
<div class="cnblogs_code">
<pre># coding=utf-<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
#</span><span style="color: rgba(128, 0, 128, 1)">1</span>.先设置编码,utf-<span style="color: rgba(0, 0, 0, 1)">8可支持中英文,如上,一般放在第一行

#</span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">.注释:包括记录创建时间,创建人,项目名称。
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>Created on <span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">5</span>-<span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)">
@author: 北京</span>-<span style="color: rgba(0, 0, 0, 1)">宏哥
Project:学习和使用将测试报告通过邮件发出去
</span><span style="color: rgba(128, 0, 0, 1)">'''
</span>#<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.导入unittest模块
import unittest
import os
</span><span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.mime.text import MIMEText
</span><span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)"> email.header import Header
import smtplib
#</span><span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">.编写测试用例和断言
def all_case():
    # 待执行用例的目录
    #case_dir </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\Users\\DELL\\PycharmProjects\\honggetest\\case</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    case_dir </span>= os.path.join(os.getcwd(), <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">case</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
    testcase </span>=<span style="color: rgba(0, 0, 0, 1)"> unittest.TestSuite()
    discover </span>=<span style="color: rgba(0, 0, 0, 1)"> unittest.defaultTestLoader.discover(case_dir,
                                                   pattern</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test*.py</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                                                   top_level_dir</span>=<span style="color: rgba(0, 0, 0, 1)">None)
    # #discover方法筛选出用例,循环添加到测试套件中
    # </span><span style="color: rgba(0, 0, 255, 1)">for</span> test_suit <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> discover:
    #   </span><span style="color: rgba(0, 0, 255, 1)">for</span> test_case <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> test_suit:
    #         #添加用力到testcase
    #         testcase.addTests(test_case)
    # print(testcase)

    testcase.addTests(discover)# 直接加载 discover    可以兼容python2和3
    print(testcase)
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> testcase


# </span>==============定义发送邮件==========<span style="color: rgba(0, 0, 0, 1)">
def send_mail(file_new):
    f </span>= open(file_new, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">rb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    mail_body </span>=<span style="color: rgba(0, 0, 0, 1)"> f.read()
    f.close()

    username </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">nXXply@ceXXx.cn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">#发件箱用户名
    password </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">XXX@@123</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">      #发件箱密码
    sender </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">noXXy@ceXXx.cn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">    #发件人邮箱
    receiver </span>= [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">hongge@XXX.cn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]#收件人邮箱
    # 邮件正文是MIMEText
    msg </span>= MIMEText(mail_body, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    # 邮件对象
    msg[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Subject</span><span style="color: rgba(128, 0, 0, 1)">'</span>] = Header(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">自动化测试报告</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).encode()
    msg[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">From</span><span style="color: rgba(128, 0, 0, 1)">'</span>] = Header(u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">测试机 &lt;%s&gt;</span><span style="color: rgba(128, 0, 0, 1)">'</span>%<span style="color: rgba(0, 0, 0, 1)">sender)
    msg[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">To</span><span style="color: rgba(128, 0, 0, 1)">'</span>] = Header(u<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">测试负责人 &lt;%s&gt;</span><span style="color: rgba(128, 0, 0, 1)">'</span>%<span style="color: rgba(0, 0, 0, 1)">receiver)
    msg[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">date</span><span style="color: rgba(128, 0, 0, 1)">'</span>] = time.strftime(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%a,%d %b %Y %H:%M:%S %z</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
    #发送邮件
    smtp </span>=<span style="color: rgba(0, 0, 0, 1)"> smtplib.SMTP()
    smtp.connect(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">smtp.mxhichina.com</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)# 邮箱服务器
    smtp.login(username, password)# 登录邮箱
    smtp.sendmail(sender, receiver, msg.as_string())# 发送者和接收者
    smtp.quit()
    print(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">邮件已发出!注意查收。</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)


# </span>======查找测试目录,找到最新生成的测试报告文件======<span style="color: rgba(0, 0, 0, 1)">
def new_report(test_report):
    lists </span>=<span style="color: rgba(0, 0, 0, 1)"> os.listdir(test_report)# 列出目录的下所有文件和文件夹保存到lists
    lists.sort(key</span>=lambda fn: os.path.getmtime(test_report + <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\\</span><span style="color: rgba(128, 0, 0, 1)">"</span> +<span style="color: rgba(0, 0, 0, 1)"> fn))# 按时间排序
    file_new </span>= os.path.join(test_report, lists[-<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">])# 获取最新的文件保存到file_new
    print(file_new)
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> file_new

</span><span style="color: rgba(0, 0, 255, 1)">if</span> __name__ == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">:
    # 返回实例
    runner </span>=<span style="color: rgba(0, 0, 0, 1)"> unittest.TextTestRunner()
    #导入第三方模块HTMLTestRunner
    import HTMLTestReportCN
    import time
    # 获取当前时间,这样便于下面的使用。
    now </span>= time.strftime(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">%Y-%m-%M-%H_%M_%S</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, time.localtime(time.time()))
    #保存生成报告的路径
    report_path </span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\Users\\DELL\\PycharmProjects\\honggetest\\report\\</span><span style="color: rgba(128, 0, 0, 1)">"</span>+now+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_result.html</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    fp </span>= open(report_path,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">wb</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
    runner </span>= HTMLTestReportCN.HTMLTestRunner(stream=<span style="color: rgba(0, 0, 0, 1)">fp,
                                           title</span>=u<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">这是我的自动化测试用例</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                                           description</span>=u<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">用例执行情况</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
                                           verbosity </span>= <span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
                                           )
    # run 所有用例
    runner.run(all_case())
    #关闭文件,记住用open()打开文件后一定要记得关闭它,否则会占用系统的可打开文件句柄数。
    fp.close()
    #测试报告文件夹
    test_path </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">C:\\Users\\DELL\\PycharmProjects\\honggetest\\report\\</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    new_report </span>=<span style="color: rgba(0, 0, 0, 1)"> new_report(test_path)
    send_mail(new_report)# 发送测试报告</span></pre>
</div>
<h2>小结</h2>
<p>1、遇到的问题及解决方法</p>
<p>(1)将生成的测试报告的保存路径和查找测试报告的路径混淆会报如下错误</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506103924189-992662445.png" alt=""></p>
<p>(2)解决方法</p>
<p>a、我们先来分析一下原因,刚开始我查了资料还觉得是不是数组越界,后来感觉不对,就复制粘贴到我的电脑报如下错误,确实找不到</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506104159395-1118007246.png" alt=""></p>
<p>b、接着Ctrl+R复制进去,运行出现如下这是才知道查找最新的测试报告遍历的根本不是一个目录而是一个文件</p>
<p>&nbsp;</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506104500599-1767031482.png" alt=""></p>
<p>c、解决办法不是文件夹修改成测试报告的文件夹是不是就可以,做如下简单地修改就可以了</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506104808209-915136186.png" alt=""></p>
<p>d、完了把test_path拷贝在我电脑和运行处,两个界面一样分别定位到测试报告。这样就可以遍历,这是不用试 一定可以成功分别如下:</p>
<p>我的电脑</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506105114358-260779565.png" alt=""></p>
<p>&nbsp;</p>
<p>运行</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506105215294-1464883342.png" alt=""></p>
<p>e、代码运行结果</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506105410258-2132774264.png" alt=""></p>
<p>f、到收件箱可以看到测试报告</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506105509077-1235636861.png" alt=""></p>
<p>&nbsp;2、发送的邮件看不到发件人和收件人,如图</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506120144481-1992485559.png" alt=""></p>
<p>&nbsp;a、解决办法:</p>
<p>加两行代码即可</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506134816260-603909728.png" alt=""></p>
<p>b、再次运行收到邮件</p>
<p><img src="https://img2018.cnblogs.com/blog/1232840/201905/1232840-20190506134912693-521929684.png" alt=""></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div id="MySignature" style="display: block">
        <div style="font-size: 13px; border: 1px dashed rgb(45, 161, 45); padding: 10px 15px; background-color: rgb(248, 248, 248)">
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家在移动端也能看到我分享的博文,现已注册个人微信公众号,扫描左下方二维码即可,欢迎大家关注,提前解锁更多测试干货!有时间会及时分享相关技术博文。
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,刚刚建立了咱们的专门的微信群交流互动群,群内会分享交流测试领域前沿知识。请您扫描中间的微信二维码进群
                </label>
                <br>
                <label style="font-weight: bold">
                        &nbsp;&nbsp;&nbsp;&nbsp;为了方便大家互动讨论相关技术问题,现已组建专门的微信群,由于微信群满100,请您扫描右下方宏哥个人微信二维码拉你进群
                        <label style="font-weight: bold; color: red; font-size: 15px">
                                (请务必备注:已关注公众号进群)平时上班忙(和你一样),所以加好友不及时,请稍安勿躁~
                        </label>
                        ,欢迎大家加入这个大家庭,我们一起畅游知识的海洋。
                </label>
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;感谢您花时间阅读此篇文章,如果您觉得这篇文章你学到了东西也是为了犒劳下博主的码字不易不妨打赏一下吧,让博主能喝上一杯咖啡,在此谢过了!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;如果您觉得阅读本文对您有帮助,请点一下左下角
               
                        “推荐”
               
                按钮,您的
                <label style="font-weight: bold; color: red; font-size: 15px">
                        “推荐”
                </label>
                将是我最大的写作动力!另外您也可以选择
               
                        【
                        <strong>
                                关注我
                        </strong>
                        】
               
                ,可以很方便找到我!
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;本文版权归作者和博客园共有,来源网址:
               
                        https://www.cnblogs.com/du-hong
               
                欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利!
        </div>
        <div style="text-align: center; margin-top: 10px">
                <p style=" font-weight: bolder; color: red; ">
                        公众号(关注宏哥)&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace; &NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        微信群(扫码进群) &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;
                        &NonBreakingSpace;&NonBreakingSpace;&NonBreakingSpace; &NonBreakingSpace;&NonBreakingSpace;客服微信
                </p>
                <img style="width: 200px;padding-right: 50px;" alt="个人微信公众号" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191119095948011-608816619.png">
                <img style="width: 200px;padding-right: 65px;" alt="微信群" src="https://img2024.cnblogs.com/blog/1232840/202506/1232840-20250610113707419-637869921.png">
                <img style="width: 200px" alt="个人微信" src="https://img2018.cnblogs.com/common/1741949/201911/1741949-20191106101257091-849954564.png">
        </div>
</div><br><br>
来源:https://www.cnblogs.com/du-hong/p/10812533.html
頁: [1]
查看完整版本: python接口自动化(二十九)--html测试报告通过邮件发出去——上(详解)