-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_tests.py
75 lines (64 loc) · 2.39 KB
/
run_tests.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author:jinzj
@contact:
@others:
@desc:
"""
import time, sys, os
sys.path.append('./test_case')
sys.path.append('./public')
from HTMLTestRunner import HTMLTestRunner
import unittest
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
from public import mail
def send_email(filename):
mail_host='****'
mail_user=mail.mail_user #邮箱名 '[email protected]'
mail_pass=mail.mail_pass #密码 ;'#¥%……'
sender=mail.sender #发送邮箱名
receivers=['****'] #收件人
message=MIMEMultipart('related')
f=open(filename,'rb')
mail_body=f.read()
att = MIMEText(mail_body,'base64','uft-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="report.html"'
message.attach(att)
f.close()
msg=MIMEText(mail_body,'html','utf-8')
message.attach(msg)
message['From']=sender
message['To'] = ",".join(receivers)
message['Subject']=Header("接口自动化测试报告",'utf-8')
smtp=smtplib.SMTP()
smtp.connect(mail_host)
smtp.login(mail_user,mail_pass)
smtp.sendmail(sender,receivers,message.as_string())
smtp.quit()
def report(testreport): #查找最新的测试报告
lists = os.listdir(testreport) #返回指定的文件夹包含的文件或文件夹的名字的列表
lists.sort(key=lambda fn: os.path.getatime(testreport + "\\" + fn))#通过sort()方法重新按时间对目录下的文件进行排序
filename = os.path.join(testreport, lists[-1])#list[-1]取最新生成的文件或者文件夹
print(filename)
return filename
if __name__ == "__main__":
# test_data.init_data() # 初始化接口测试数据
# 指定测试用例为当前文件夹下的 test_case 目录
test_dir = './test_case'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='*_test.py')
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = './report/' + now + '_result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='qiaoba testing Interface Test Report',
description='The results are following:')
runner.run(discover)
fp.close()
test_report = './report' #定义报告文件目录
rep = report(test_report)
send_email(rep)