飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • AngularJS教程
  • UEditor使用文档
  • ThinkPHP5.0教程

测试平台系列(88) 完成邮件通知功能(附赠精美邮件模板)

时间:2022-01-21  作者:we8fans  

大家好~我是米洛

我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持。

欢迎关注我的公众号米洛的测开日记,获取最新文章教程!

回顾

书接上回,我们找准了一款看似不错的邮件模板,但由于数据都是死的,所以我们需要获取测试报告产生的数据,并渲染HTML模板

这节我们就来彻彻底底完善这块功能。

效果图

这次数据已经真实,而且有定时任务直接跑了发送出来,相当靠谱

编写邮件模块

由于之前yagmail不是太好用,所以我们需要改写send_msg方法,由于比较简单,我就直接上代码了。

不过在此之前呢,我们需要先去配置文件里面加上一个字段:

其实smtplib提供了From的选项,我本来想叫Pity自动化测试平台,但是那样的话邮件死活发不出去,遂放弃。

    @staticmethod
    def send_msg(subject, content, attachment=None, *receiver):
        configuration = 域名config()
        data = 域名("email")
        sender = 域名("sender")
        to = 域名("to")
        message = MIMEText(content, \'html\', \'utf-8\')
        message[\'From\'] = sender
        message[\'To\'] = Header(to, \'utf-8\')
        message[\'Subject\'] = Header(subject, \'utf-8\')

        try:
            smtp = 域名()
            域名ect(域名("host"))
            # 我们用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息。
            域名debuglevel(1)
            域名n(sender, 域名("password"))
            域名mail(sender, list(receiver), 域名tring())
        except Exception as e:
            raise Exception(f"发送测试报告邮件失败: {e}")

新的send_msg方法很简单,在保持参数一致的情况下,代码量比yagmail多了很多。

大概思路就是先封装MIMETEXT,并设置为html模式,接着把收件人发件人主题以及内容等数据都放入其中,最后通过sendmail发送邮件。

改造run_test_plan方法

  • 添加执行人参数

目前我们的测试计划还不支持手动执行,一方面为了适配手动执行,一方面为了在邮件体现执行人信息,所以我们在run_test_plan加上执行人这个参数:

执行人默认是0,也就是CPU(小时候打小霸王的时候的感觉)

  • 改写run_multiple方法

    run_multiple这个方法是我们测试计划执行的核心方法,由于我们测试计划是支持多环境的,并且一个测试环境对应一份测试报告。

    我们在执行完一个测试计划可能出现多个报告链接,每个环境也有自己的通过率这些数据。

    所以我们需要记录一个map,里面存放env => 测试结果的映射,最终返回。

    由于我们是异步执行,所以我们在外部设定一个map,当做参数传递进去,由于引用传递的原理,函数执行完毕,我们的map也更新好了。

顺理成章的,run_multiple方法里面也需要改造,它应该接收新的map参数,但为了不影响原先的功能,所以它可传可不传。

下面是获取执行人姓名的操作:

接着是run_multiple在返回之前的改造:

生成真实html

上述操作都是为了获取测试报告需要的数据,获取了之后我们还得利用jinja2渲染html。

这里我放上原生html:

<!DOCTYPE html>
<html>
<head>
	<title>
		测试报告
	</title>
</head>
<body>
<div>
    <includetail>
        <div align="center">
            <div class="open_email" style="margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;">
                <div>
                    <br>
                    <span class="genEmailContent">
                        <div id="cTMail-Wrap"
                             style="word-break: break-all;box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:\'helvetica neue\',PingFangSC-Light,arial,\'hiragino sans gb\',\'microsoft yahei ui\',\'microsoft yahei\',simsun,sans-serif">
                            <div class="main-content" style="">
                                <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse">
                                    <tbody>
                                    <tr style="font-weight:300">
                                        <td style="width:3%;max-width:30px;"></td>
                                        <td style="max-width:600px;">
                                            <div id="cTMail-logo" style="width:92px; height:25px;">
                                                <a href="">
                                                    <img border="0" src="https://域名/woodywrx/picture/raw/master/2021-11-24/1637761462006-域名"
                                                         style="width:36px; height:36px;display:block">
                                                </a>
                                            </div>
                                            <p style="height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;"></p>
                                            <div id="cTMail-inner" style="background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;">
                                                <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;">
                                                    <tbody>
                                                    <tr style="font-weight:300">
                                                        <td style="width:3.2%;max-width:30px;"></td>
                                                        <td style="max-width:480px;text-align:left;">
                                                            <h1 id="cTMail-title" style="font-size: 20px; line-height: 36px; margin: 0px 0px 22px;">
                                                                【<strong>{{ env }}</strong>】测试计划 【<strong>{{ plan_name }}</strong>】执行结果: <strong>{{plan_result}} </strong>
                                                            </h1>

                                                            <p id="cTMail-userName" style="font-size:14px;color:#333; line-height:24px; margin:0;">
                                                                尊敬的pity用户,您好!
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    本次测试计划共执行用例<strong>{{ total }}</strong>条。
                                                                </span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    由<strong>{{ executor }}</strong>于{{ start_time }}开始执行,共耗时{{cost}}秒。
                                                                </span>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    成功数量 <strong style="color: #67C23A">{{ success }}</strong>
                                                                </span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    失败数量 <strong style="color: #F56C6C">{{ failed }}</strong>
                                                                </span>
                                                            </p>
                                                           <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    出错数量 <strong style="color: #E6A23C">{{ error }}</strong>
                                                                </span>
                                                            </p>
                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    跳过数量 <strong style="color: #409EFF">{{ skip }}</strong>
                                                                </span>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">查看详细测试报告,可点击下方链接。
                                                                    <span style="font-weight: bold;">如有打扰,请您谅解。</span>
                                                                </span>
                                                            </p>

                                                            <p class="cTMail-content"
                                                               style="font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;">
                                                                <a id="cTMail-btn" href="{{report_url}}" title=""
                                                                   style="font-size: 16px; line-height: 45px; display: block; background-color: rgb(0, 164, 255); color: rgb(255, 255, 255); text-align: center; text-decoration: none; margin-top: 20px; border-radius: 3px;">
                                                                    点击此处查看完整报告
                                                                </a>
                                                            </p>

                                                            <p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;">
                                                                <span style="color: rgb(51, 51, 51); font-size: 14px;">
                                                                    <br>
                                                                    无法正常显示?请复制以下链接至浏览器打开:
                                                                    <br>
                                                                    <a href="{{report_url}}" title=""
                                                                       style="color: rgb(0, 164, 255); text-decoration: none; word-break: break-all; overflow-wrap: normal; font-size: 14px;">
                                                                        这里是测试报告链接
                                                                    </a>
                                                                </span>
                                                            </p>
                                                        </td>
                                                        <td style="width:3.2%;max-width:30px;"></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </div>

                                            <div id="cTMail-copy" style="text-align:center; font-size:12px; line-height:18px; color:#999">
                                                <table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse">
                                                    <tbody>
                                                    <tr style="font-weight:300">
                                                        <td style="width:3.2%;max-width:30px;"></td>
                                                        <td style="max-width:540px;">

                                                            <p style="text-align:center; margin:20px auto 14px auto;font-size:12px;color:#999;">
                                                                此邮件由pity自动发出,请勿回复。
                                                            </p>

                                                            <p id="cTMail-rights" style="max-width: 100%; margin:auto;font-size:12px;color:#999;text-align:center;line-height:22px;">
                                                                <img border="0" src="https://域名/woodywrx/picture/raw/master/2021-8-7/1628267097936-域名"
                                                                     style="width:84px; height:84px; margin:0 auto;">
                                                                <br>
                                                                关注测试开发坑货,了解pity更多内容
                                                                <br>
                                                            </p>
                                                        </td>
                                                        <td style="width:3.2%;max-width:30px;"></td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </td>
                                        <td style="width:3%;max-width:30px;"></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </span>
                </div>
            </div>
        </div>
    </includetail>
</div>

</body>
</html>

上述代码在pity/templates/域名文件内。

接着编写渲染html的方法:

其中kwargs是传递对应的参数给html。

之前也说过一个邮件对应一个地址,所以send_msg调用的地方也得跟着修改。

这样就是一个环境对应一封邮件

至此,我们的邮件推送功能就全部完成了。

链接能正常点击,还要啥自行车

今天的内容就到这里了,咱们下期见。

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。