python发邮件怎么用服务器每天发_定时任务与Python发送邮件(Windows平台)

前言

上一篇文章《postman接口用例批量执行(Newman)》已经在本地生成了测试报告,这时候需要设置一个定时任务每天早上自动执行测试用例,并且发送邮件给指定人员,这里用python执行cmd命和模拟发送邮件,然后设置widows计划任务即可每天自动运行。(Linux使用crontab命令设置定时任务)

用到的python库smtplib

email

os

python执行cmd命令1

2

3

4def test_command():

'''调用cmd执行接口测试命令'''

command = r'newman run E:api_collectiontest.postman_collection.json -e E:api_collection测试环境.postman_environment.json -r html,json,junit,cli --reporter-html-export E:api_collectionreport.html --reporter-json-export E:api_collectionreport.json --reporter-junit-export E:api_collectionreport.xml'

print(os.system(command))

将执行接口测试用例的命令赋值给变量command,在通过print(os.system(command))方法

来执行命令,生成测试报告,因为Newman已经配置好了环境变量,所以不用关注在哪个路径下执行命令。

设计在本地获取测试报告的函数1def html_report(testreport):

这个testreport参数是测试报告所在的目录

1.罗列目录下的文件1lists = os.listdir(testreport)

2.找到html测试报告1

2

3

4

5

6

7for i in lists:

if '.html' in i:

html_report = os.path.join(testreport,i) #获取html格式的测试报告

print(html_report)

return html_report

else:

pass

for循环来遍历目录下所有报告的文件名,通过文件后缀是否是.html来找到html测试报告,os.path.join(testreport,i)方法把文件目录和文件名合并成完整的路径。

设计发送邮件的函数1def send_mail(html_report):

html_report参数就是上一个函数的返回值。

1.创建一个带附件的实例1

2

3

4msg = MIMEMultipart()

msg['Subject'] = Header('自动化测试报告','utf-8') #这个是邮件主题名称

msg['From'] = Header('peili','utf-8') #发送者名字

msg['To'] = Header('ceshi','utf-8') #接收者名字

2.邮件正文内容1msg.attach(MIMEText('测试组8月分享(报告在附件)','html','utf-8'))

MIMEText的第二个参数是发送的文本类型,有html,plain,base64等。

3.添加一个附件1

2

3

4file1 = MIMEText(open(html_report,'rb').read(),'base64','utf-8')

file1['Content-Type'] = 'application/octet-stream'

file1["Content-Disposition"] = 'attachment; filename="report.html"' #这里的filename就是邮件中附件的名字,可以自己命名

msg.attach(file1)

添加多个附件的话就再复制这四行代码多写几个,修改open(xx,'rb')就好。

4.连接邮件服务器并发送邮件1

2

3

4smtp = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465) #使用的是腾讯企业邮箱服务器,端口465

smtp.login('[email protected]', 'password') #两个参数分别是发送者的企业邮箱账号和密码

smtp.sendmail(sender, receiver, msg.as_string()) #前两个参数分别是发送邮箱和接收邮箱

smtp.quit()

1.smtp.sendmail(sender, receiver, msg.as_string())第二个参数receiver邮件接收者可以是一个列表,达到群发的目的。

2.这里我用的是腾讯企业邮箱,如果使用的是个人QQ邮箱的话,登陆密码不能直接写邮箱密码,需要填写QQ邮箱的授权码,登陆网页版QQ邮箱,在设置->账户里面找到,并记得开启下图所示的服务。

注:经过平时的使用发现个人QQ邮箱发送邮件会偶尔失败,即使设置好了服务和第三方授权码仍然会有授权码失效等莫名其妙的问题,不知道是不是QQ邮箱的什么安全策略导致的。

设置定时任务

前面已经把python脚本编写完毕,包括从测试用例执行到发送邮件,现在设置一个定时任务,每天自动运行就可以了,Windows7及以前使用at命令,windows8及以后使用schtasks命令:1

2

3

4

5Windows7及以前

at 8:00 python E:api_collectionsend_email.py

windows8及以后

schtasks /create /tn api_test /tr "python E:api_collectionsend_email.py" /sc daily /st 8:00

意思就是每天早上8点钟执行这个python脚本。

不足之处

可以看到利用python能完成自动执行测试用例并发送邮件,但是需要写很多代码,也不能在线实时监控,因此这种方式并不是最好的方式,下一篇文章将会写一些Jenkins的东西,通过Jenkins做持续集成要方便的多,想要真正做自动化早晚都要接触Jenkins的。

完整代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59import smtplib

from email.mime.text import MIMEText

from email.header import Header

from email.mime.multipart import MIMEMultipart

import os

testreport ='E:/api_collection/report'

def ():

'''调用cmd执行接口测试命令'''

command = r'newman run E:api_collectiontest.postman_collection.json -e E:api_collection测试环境.postman_environment.json -r html,json,junit,cli --reporter-html-export E:api_collectionreportreport.html --reporter-json-export E:api_collectionreportreport.json --reporter-junit-export E:api_collectionreportreport.xml'

print(os.system(command))

def html_report(testreport):

'''在本地找到html测试报告'''

lists = os.listdir(testreport)

for i in lists:

if '.html' in i:

html_report = os.path.join(testreport,i) #获取html格式的测试报告

print(html_report)

return html_report

else:

pass

def send_mail(html_report):

'''发送邮件'''

#创建一个带附件的实例

msg = MIMEMultipart()

msg['Subject'] = Header('自动化测试报告','utf-8') #这个是邮件主题名称

msg['From'] = Header('peili','utf-8')

msg['To'] = Header('ceshi','utf-8')

#邮件正文内容

msg.attach(MIMEText('瑞云测试组接口测试8月分享(报告在附件)','html','utf-8')) #第二个参数是邮件格式,可以换成其他的比如说'html','base64','plain'等

#添加附件

file1 = MIMEText(open(html_report,'rb').read(),'base64','utf-8')

file1['Content-Type'] = 'application/octet-stream'

file1["Content-Disposition"] = 'attachment; filename="report.html"' #这里的filename就是邮件中附件的名字,可以自己命名

msg.attach(file1)

try:

smtp = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465) #QQ邮箱发送服务器以及端口,SMTP默认端口是25,这里改成465

smtp.login('password') #如果是QQ邮箱的话,第二个参数不是直接用的密码,用的是QQ邮箱的授权码

smtp.sendmail(sender, receiver, msg.as_string()) #前两个参数分别是发送邮箱和接收邮箱

smtp.quit()

print('测试报告邮件发送成功')

except smtplib.SMTPException as e:

print(e)

if __name__ == '__main__':

test_command()

html_report = html_report(testreport)

send_mail(html_report)

相关文章peili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值