Selenium + Python 自动化测试14(发送报告)

我们的目标是:按照这一套资料学习下来,大家可以独立完成自动化测试的任务。

上一篇我们讨论了使用HTMLTestRunner 生成HTML报告的方法。

本篇文章我们接着讲生成HTML报告是否可以自动邮件发送出去,提高我们测试报告的及时性,方便性,避免自己手动操作发送。

1、SMTP介绍

SMTP: simple mail transfer protocol 简单邮件传输协议。是一组由源地址到目的地址传送邮件的规则。

python 的smtplib 模块提供一个很方便的路径用于发送电子邮件。它是对SMTP进行封装而来。我们可以SMTP对象的sendmail 来发送邮件。可以先使用help()查看使用方式:

>>> from smtplib import SMTP

>>> help(SMTP)

connect(self, host='localhost', port=0, source_address=None)

| Connect to a host on a given port.

|

| If the hostname ends with a colon (`:') followed by a number, and

| there is no port specified, that suffix will be stripped off and the

| number interpreted as the port number to use.

|

| Note: This method is automatically invoked by init, if a host is

| specified during instantiation.

login(self, user, password, *, initial_response_ok=True)

| Log in on an SMTP server that requires authentication.

|

| The arguments are:

| - user: The user name to authenticate with.

| - password: The password for the authentication.

|

| Keyword arguments:

| - initial_response_ok: Allow sending the RFC 4954 initial-response

| to the AUTH command, if the authentication methods supports it.

|

| If there has been no previous EHLO or HELO command this session, this

| method tries ESMTP EHLO first.

|

| This method will return normally if the authentication was successful.

|

| This method may raise the following exceptions:

|

| SMTPHeloError The server didn't reply properly to

| the helo greeting.

| SMTPAuthenticationError The server didn't accept the username/

| password combination.

| SMTPNotSupportedError The AUTH command is not supported by the

| server.

| SMTPException No suitable authentication method was

| found.

sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])

| This command performs an entire mail transaction.

|

| The arguments are:

| - from_addr : The address sending this mail.

| - to_addrs : A list of addresses to send this mail to. A bare

| string will be treated as a list with 1 address.

| - msg : The message to send.

| - mail_options : List of ESMTP options (such as 8bitmime) for the

| mail command.

| - rcpt_options : List of ESMTP options (such as DSN commands) for

| all the rcpt commands.

1)connect(host,port)

host:指定连接的邮箱服务器

port: 指定连接的邮箱服务器端口号

2)login(user,password)

登录邮箱的用户和密码

3)sendmail(from_addr,to_addr,msg...)

from_addr:邮件发送者地址

to_addr:邮件接受者邮箱

msg:发送的消息

2、发送报告到指定邮箱

1)编写执行侧用例集的代码

这部分参考之前的,我们上一节已经讨论过的:

复制代码
#定义测试用例集的目录

test_dir ='./'

report_dir ='./report'  #测试报告地址

discover =unittest.defaultTestLoader.discover(test_dir,pattern="test_*.py")#所有要执行的文件



if __name__ =='__main__':

    now =time.strftime("%Y-%m-%d %H_%M_%S")   #当前时间,格式是年月日时分秒

    file_name =report_dir+'/' + now +'result.html'   #报告名称,加上当前时间避免重复

    fp =open(file_name,'wb')      #打开报告文件,读写权限



    runner =HTMLTestRunner(stream=fp,title="Swag Labs 网站测试报告",description="用例测试情况:")#HTML报告设置

    runner.run(discover)  #执行测试案例

    fp.close()   #关闭报告文件
2、编写发送部分

编写对应的发送邮件的函数。

复制代码
from email.mime.text importMIMEText

fromemail.header importHeader



#定义发送邮件的函数

def send_report_by_mail(file_name):f=open(file_name,'rb')  #打开文件

    report_body=f.read()

    f.close()



    msg=MIMEText(report_body,'html','utf-8') #内容

    msg['Subject']=Header('Swag Labs 网站测试报','utf-8')



    smtp =smtplib.SMTP()

    smtp.connect('smtp.163.com') #发送邮箱服务器

    smtp.login('用户名@163.com','密码')

    smtp.sendmail('用户名@163.com','接收邮件用户名@163.com',msg.as_string())

    smtp.quit()

    print("报告已通过邮件发送")



#定义测试用例集的目录

test_dir ='./'

report_dir ='./report'  #测试报告地址

discover =unittest.defaultTestLoader.discover(test_dir,pattern="test_*.py")#所有要执行的文件



if __name__ =='__main__':

    now =time.strftime("%Y-%m-%d %H_%M_%S")   #当前时间,格式是年月日时分秒

    file_name =report_dir+'/' + now +'result.html'   #报告名称,加上当前时间避免重复

    fp =open(file_name,'wb')      #打开报告文件,读写权限



    runner =HTMLTestRunner(stream=fp,title="Swag Labs 网站测试报告",description="用例测试情况:")#HTML报告设置

    runner.run(discover)  #执行测试案例

    fp.close()   #关闭报告文件



    send_report_by_mail(file_name)   #发送报告

如下图所示发送邮件成功:

收到了邮件如下:

今天就先学习到这里。

每天进步一点点,加油!

相关推荐
IT学长编程8 小时前
计算机毕业设计 基于Python的热门游戏推荐系统的设计与实现 Django 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试】
大数据·python·django·毕业设计·课程设计·毕业论文
Ashlee_code9 小时前
什么是TRS收益互换与场外个股期权:从金融逻辑到系统开发实践
大数据·人工智能·python·金融·系统架构·清算·柜台
今天没有盐9 小时前
Python编程实战:日期处理与数学算法综合练习
python·pycharm·编程语言
交换机路由器测试之路9 小时前
发包工具anysend使用手册
网络·测试工具·ipv6·发包工具
宸津-代码粉碎机9 小时前
Java内部类内存泄露深度解析:原理、场景与根治方案(附GC引用链分析)
java·开发语言·jvm·人工智能·python
weixin_3077791310 小时前
Python编码规范之字符串规范修复程序详解
开发语言·python·代码规范
爬台阶的蚂蚁10 小时前
使用 UV 工具管理 Python 项目的常用命令
python·uv
郝学胜-神的一滴10 小时前
深入理解 Python 的 __init_subclass__ 方法:自定义类行为的新方式 (Effective Python 第48条)
开发语言·python·程序人生·个人开发
王景程10 小时前
让IOT版说话
后端·python·flask
JJJJ_iii10 小时前
【机器学习11】决策树进阶、随机森林、XGBoost、模型对比
人工智能·python·神经网络·算法·决策树·随机森林·机器学习