Requests方法 -- 项目实现自动发送邮件

Teachertao 2019-07-13 原文

Requests方法 — 项目实现自动发送邮件

"""
1.discover方法里面有三个参数:
-case_dir:这个是待执行用例的目录。
-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本。
-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。
2.discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里;
这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。
"""

import unittest
import os,time
import HTMLTestRunner
from tomorrow import threads
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

#unittest_test目录,下有case和report
cur_path = os.path.dirname(__file__)

def all_case(casename="case",rule="test*.py"):
'''第一加载所有的测试用例'''
case_path = os.path.join(cur_path,casename) #用例路径拼接
#如果不存在case文件夹,自动创建
if not os.path.exists(case_path):os.mkdir(case_path)
discover = unittest.TestLoader().discover(
casename,
pattern=rule,
top_level_dir=None
)
return discover

# def getNowtime():
# return time.strftime("%Y-%M-%D %H-%M-%S",time.localtime(time.time()))

def report():
"""第二执行所有用例,并把结果写入HTML测试报告中"""
# now = time.strftime("%Y-%M-%D %H-%M-%S")
report_path = os.path.join(cur_path,"report") #report文件夹
if not os.path.exists(report_path):os.mkdir(report_path)
report_abspath = os.path.join(report_path,"result.html") # html报告文件路径
# file = os.path.join(os.path.dirname(__file__), "Report", "testReport.html")
# print("report_path:%s"%report_abspath)
with open(report_abspath, "wb") as fp:
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'自动化测试报告,测试结果如下:',
description=u'用例执行情况:')
# 调用add_case函数返回值
runner.run(all_case())
return report_abspath

def send_mail():
"""第三发送测试报告"""
# ----------1.跟发件相关的参数------

smtpserver = "smtp.163.com" # 发件服务器
# smtpserver = "smtp.qq.com"
port = 25 # 菲SSL协议端口号
# sender = "XXXX" # 账号
sender = "自己163邮箱账号"
psw = "自己的邮箱密码"
# psw = "wmqtqbtnmyamhfjd" # 密码
receiver = "xxxxx@qq.com" # 单个接收人也可以是 list
# receiver = ["xxxxx@qq.com"] # 多个收件人 list 对象

# ----------2.编辑邮件的内容------
# 读文件
# file_path = "Result.html"
# with open(file_path, "rb") as fp:
# mail_body = fp.read()
with open(report(),"rb") as f:
mail_body = f.read()
msg = MIMEMultipart()
msg["from"] = sender # 发件人
msg["to"] = receiver # 收件人
# msg["to"] = ";".join(receiver) # 多个收件人 list 转 str
msg["subject"] = "我的主题报告-test" # 主题

# 正文
body = MIMEText(mail_body, "html", "utf-8")
msg.attach(body)

# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="test_report.html"'
msg.attach(att)

# ----------3.发送邮件------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 连服务器
smtp.login(sender, psw)
except:
smtp = smtplib.SMTP_SSL(smtpserver, port) # QQ 邮箱
smtp.login(sender, psw) # 登录
smtp.sendmail(sender, receiver, msg.as_string()) # 发送
smtp.quit()


def main():
send_mail()

if __name__ == '__main__':
# runner = unittest_1.TextTestRunner()
# runner.run(all_case())
main()

# report_abspath = os.path.join(report_path, "result.html") # html报告文件路径
# fp = open(report_abspath, "wb")
# runner = HTMLTestRunner.HTMLTestRunner(
# stream=fp,
# title=u'自动化测试报告,测试结果如下:',
# description=u'用例执行情况:')
# # 调用add_case函数返回值
# runner.run(all_case())
# fp.close()




发表于 2019-07-13 23:15 Teacher涛 阅读() 评论() 编辑 收藏

 

版权声明:本文为Teachertao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Teachertao/p/11182578.html

Requests方法 -- 项目实现自动发送邮件的更多相关文章

  1. XDK html development — Cross Domain Request

        Two days ago, I came across a problem. After buildi […]...

  2. Requests 方法 — post请求操作实践

    Requests 方法 — post请求操作实践 1、登录Jenkins抓包 ,小编的Jenkin […]...

  3. Python网页解析库:用requests-html爬取网页

    Python网页解析库:用requests-html爬取网页1. 开始Python 中可以进行网页解析的库有很多,常见的有 BeautifulSoup 和 lxml 等。在网上玩爬虫的文章通常都是介绍 BeautifulSoup 这个...

  4. scrapy参数meta传递item的用法,结合deepcopy来理解

    scrapy.Request(url[,callback,method=”GET”,h […]...

  5. 基于promise对小程序http请求方法封装

      原因是我不想每次请求都复制粘贴那么长的请求地址,所以我把前边那一坨请求地址作为基础地址,只传后台给的路由就 […]...

  6. 如何巧妙的利用selenium和requests组合来进行操作需要登录的页面

    如何巧妙的利用selenium和requests组合来进行操作需要登录的页面 一、在这里selenium的作用 […]...

  7. HTTP 400 错误 – 请求无效 (Bad request)

    在ajax请求后台数据时有时会报 HTTP 400 错误 – 请求无效 (Bad request) […]...

  8. 使用 python 压缩 png 图片,高达 80% 压缩率,肉眼无差异(四):使用 requests 库上传

    在网上有很多使用 python 的 pillow 库进行图片压缩的教程,使用简单,但是压缩效果存在明显的色彩不 […]...

随机推荐

  1. Linux操作系统基础知识学习

    Linux操作系统概述 https://cloud.tencent.com/developer/article […]...

  2. 分集技术及应用

    分集技术及应用1 分集接收的概念  在移动通信系统中,移动台经常工作在各种复杂的地理环境中,移动的方向和速度是 […]...

  3. spss

      编辑 SPSS(Statistical Product and Service Solutions),“统 […]...

  4. Hadoop HA搭建

    在搭建高可用集群是需要用到zookeeper因此我们要先搭建zookeeperzookeeper搭建(搭建过程相对简单,但是要细心!!)1、上传安装包到master并解压 tar -xvf zookeeper-3.4.6.tar.g...

  5. 微服务简介

    单体架构 所有代码都跑在一起tomcat ,代码量达到一个数量级基本上启动一次就的等20-30分钟。   微服 […]...

  6. 使用python来批量抓取网站图片

    今天”无意”看美女无意溜达到一个网站,发现妹子多多,但是可恨一个page只显示一张或两 […]...

  7. 我面向 Google 编程,他面向薪资编程

    面试官:同学,说一说面向对象有什么好处? 神仙开发者:我觉的面向对象编程没有什么好处。 面试官:为什么(摊手、 […]...

  8. 横向TimePicker-带三角指示器

      转自http://www.apkbus.com/ 值得推荐的活动:1.写博客推荐高薪工作 写博客送小米2代 […]...

展开目录

目录导航